`
JMaCan
  • 浏览: 14467 次
  • 性别: Icon_minigender_1
  • 来自: 大连
文章分类
社区版块
存档分类
最新评论

将System.out转换成PrintWriter

 
阅读更多

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,以便开启自动清空功能;否则,你可能看不到输出。

分享到:
评论

相关推荐

    JAVA程序中FTP的连接以及上传文件

    System.out.println("disconnect success"); } catch (IOException ex) { System.out.println("not disconnect"); System.out.println(ex); } } public void upload() { //this....

    关于io的一些代码

    System.out.println("文件不存在"); System.exit(0); } File tar=new File(tagFile); if(!tar.exists()) { try { tar.createNewFile(); } catch (IOException e) { // TODO Auto-...

    RSA算法Java实现

    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....

    java 实现多线程服务器

    System.out.println ("Server starting...\n"); //使用8000端口提供服务 ServerSocket server = new ServerSocket (8000); while (true) { //阻塞,直到有客户连接 Socket sk = server.accept (); System....

    zhifuba-demo.rar

    System.out.println(req.getParameter("outTradeNo"));//交易编号 System.out.println(req.getParameter("subject"));//商品 System.out.println(req.getParameter("totalAmount"));//总价 System.out....

    桌面管家源码(java)

    System.out.print("create fail!"); } } public void Ccreateconnect(String ip) { try { socket=new Socket(ip,9999);//创建套接字 } catch(Exception e) { System.out.print("创立连接失败!"); } ...

    人工智能-项目实践-搜索引擎-java实验1-实现搜索引擎的倒排索引数据结构

    如果写文本文件,推荐使用PrintWriter,当创建好PrintWriter对象后,调用其println和print方法可以将字符串一行行的写入到文本文件,使用方法与System.out.println, System.out.print完全一样 具体使用方法,请见...

    TCP编程——网络编程详细源码

    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(); } }

    wangluo java课程设计

    System.out.println("服务器套字节创建完毕."); while(true){ Socket socket=server.accept();//若客户机提请求,socket连接 new Chat_Thread(socket).start();//启动线程 bMan.add(socket);//添加套接字 bMan...

    聊天的终端和服务器.txt

    System.out.println("服务器端已运行,等待客户端连接:"); Socket socket = ss.accept(); //接收客户端的连接请求; BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream())); ...

    高级java java 代码

    BufferedReader sin=new BufferedReader(new InputStreamReader(System.in)); PrintWriter os =new PrintWriter(socket.getOutputStream()); String readline; readline =sin.readLine(); while(!readline....

    Thinking in Java 4th Edition

    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(); }

    数据库的基本操作前端加后端.zip

    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使用实例

    Java Scanner File PrintWriter使用实例

    java经典面试2010集锦100题(不看你后悔)

    (8) System.out.println("结果是:"+a); (9) } (10)} 下面的说法中正确的是:(选择1项) A) 程序运行到第6行会出现编译错误,因为c为char类型,不能把一个汉字赋给一个字符型。 B) 程序运行到第7行会出现编译...

    JAVAssd3选择题答案全

    //这种运算是顺序进行的,试试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 ...

    socket编程集萃

     System.out.println(in.readLine());  }  server.close();  }  }  客户机代码则是接受客户键盘输入,并把该信息输出,然后输出"End"用来做退出标识。  这个程序只是简单的两台计算机之间的通讯,如果...

Global site tag (gtag.js) - Google Analytics