黑马程序员技术交流社区

标题: 重定位Out至文件,输出后文件为空,求解! [打印本页]

作者: 真实姓名被注册    时间: 2015-7-20 13:37
标题: 重定位Out至文件,输出后文件为空,求解!
  1. import java.util.*;
  2. import java.io.*;
  3. public class Temp{
  4. public static void main(String[] args){
  5. File file=new File("hehe.txt");
  6. try {
  7. System.setOut(new PrintStream(
  8. new BufferedOutputStream(
  9. new FileOutputStream("hehe.txt"))));
  10. } catch (FileNotFoundException e) {
  11. // TODO Auto-generated catch block
  12. e.printStackTrace();
  13. }
  14. System.out.println("abc");
  15. }
  16. }
复制代码
请教,为什么程序运行后,文件"hehe.txt"中没有任何内容?
作者: 真实姓名被注册    时间: 2015-7-20 13:42
明白了,需要加句System.out.flush();
作者: 耀阳圣尊    时间: 2015-7-20 13:56
  1.                 PrintStream ps= null;
  2.                
  3.                 File file = new File("d:\\hehe.txt");
  4.                
  5.                 ps = new PrintStream(new FileOutputStream(new File(

  6.                 "d:\\hehe.txt"), true));
  7.                
  8.                 System.setOut(ps);

  9.                 System.out.println("lalalal hei ma cheng xu yuan ");
复制代码






欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2