System.out 是一个PrintStream, 而PrintStream是一个OutputStream。PrintWriter有一个可以接受OutputStream作为参数的构造器。因此,只要需要,就可以使用那个构造器把System.out转换成PrintWriter:
代码:
import java.io.*;
public class ChangeSystemOut {
public static void main(String args[]){
PrintWriter out=new PrintWriter(System.out, true);
out.println("Hello World");
}
}
运行结果:
Hello World
重要的是要使用有两个参数的PrintWriter的构造函数,并将第二个参数设为true,以便开启自动清空功能;否则,你可能看不到输出。
分享到:
相关推荐
System.out.println("disconnect success"); } catch (IOException ex) { System.out.println("not disconnect"); System.out.println(ex); } } public void upload() { //this....
System.out.println("文件不存在"); System.exit(0); } File tar=new File(tagFile); if(!tar.exists()) { try { tar.createNewFile(); } catch (IOException e) { // TODO Auto-...
System.out.println("d= " + d); System.out.println("n= " + n); BigInteger m = c.modPow(d, n); // 显示解密结果 System.out.println("密文m= " + m); PrintWriter out =new PrintWriter("E:\\miwen....
System.out.println ("Server starting...\n"); //使用8000端口提供服务 ServerSocket server = new ServerSocket (8000); while (true) { //阻塞,直到有客户连接 Socket sk = server.accept (); System....
System.out.println(req.getParameter("outTradeNo"));//交易编号 System.out.println(req.getParameter("subject"));//商品 System.out.println(req.getParameter("totalAmount"));//总价 System.out....
System.out.print("create fail!"); } } public void Ccreateconnect(String ip) { try { socket=new Socket(ip,9999);//创建套接字 } catch(Exception e) { System.out.print("创立连接失败!"); } ...
如果写文本文件,推荐使用PrintWriter,当创建好PrintWriter对象后,调用其println和print方法可以将字符串一行行的写入到文本文件,使用方法与System.out.println, System.out.print完全一样 具体使用方法,请见...
System.out.println("Side of Square Received: " + side); double squareArea = side * side; String str = "Side: " + side + " Square:" + squareArea; PrintWriter printWriter = new ...
自己写的通讯软件 public void go(){ clientOutputStreams = new ArrayList<PrintWriter>();... System.out.println("got a connection"); } }catch(Exception ex){ ex.printStackTrace(); } }
System.out.println("服务器套字节创建完毕."); while(true){ Socket socket=server.accept();//若客户机提请求,socket连接 new Chat_Thread(socket).start();//启动线程 bMan.add(socket);//添加套接字 bMan...
System.out.println("服务器端已运行,等待客户端连接:"); Socket socket = ss.accept(); //接收客户端的连接请求; BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream())); ...
BufferedReader sin=new BufferedReader(new InputStreamReader(System.in)); PrintWriter os =new PrintWriter(socket.getOutputStream()); String readline; readline =sin.readLine(); while(!readline....
What’s Inside Preface 1 Java SE5 and SE6 .................. 2 Java SE6 ............................................The 4th edition...........................Changes ...........................................
new java.io.PrintWriter(System.out, true); At the end of execution of the Java statement, a call to stdOut.println() would result in which of the following? I. Output of a new line character ...
System.out.println("关键字:"+b+" 检索完成"); } if( myFile != null) myFile.close(); if( resultFile != null) resultFile.close(); } catch (ParserException e) { e.printStackTrace(); }
System.out.println(json); out.flush(); out.close(); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doGet(req,...
Java Scanner File PrintWriter使用实例
(8) System.out.println("结果是:"+a); (9) } (10)} 下面的说法中正确的是:(选择1项) A) 程序运行到第6行会出现编译错误,因为c为char类型,不能把一个汉字赋给一个字符型。 B) 程序运行到第7行会出现编译...
//这种运算是顺序进行的,试试System.out.println(x + y + “1”); (a) 7 (b) 5 2 (c) 5+2 (d) 52 6.What is the right way to handle abnormalities in input on Java? (a) By handling these problems by ...
System.out.println(in.readLine()); } server.close(); } } 客户机代码则是接受客户键盘输入,并把该信息输出,然后输出"End"用来做退出标识。 这个程序只是简单的两台计算机之间的通讯,如果...