import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintStream;

public class B ...{
public static void main(String[] args) ...{
try ...{
System.setOut(new PrintStream(new FileOutputStream(new File("c:/test.txt"))));
System.out.println("haha");
} catch (Exception e) ...{
e.printStackTrace();
}
}
} 

*******************************************************************************************************************************

System.err和System.out 就是错误输出和标准输出
如果你用LOG4J记录日志的话,且设定错误等级的话
System.err的输出是将记录到日志中
*******************************************************************************************************************************
在你setout之前,把System.out赋值给一个PrintStream对象;如:
PrintStream new = new PrintStream(....);
PrintStream old = System.out;
System.setout(new);
......
.....
System.setout(old);//这样就恢复到默认输出状态了.
*******************************************************************************************************************************
一个PrintStream 可以被创建为自动刷新的;这意味着当一个字节数组(bytearray)被写入,或者某个println 方法被调用,或者一个换行字符或字节(‘ ‘)被写入之后,PrintStream 类型的flush 方法就会被自动地调用。//第一种 System.out.write(b); //第二种要flush for(int i = 0; i <greetin...
本文介绍如何使用Java的PrintStream类来重定向程序的标准输出(System.out)到文件,并提供了具体的代码示例。此外还介绍了如何恢复默认输出以及PrintStream类的一些特性。
1014

被折叠的 条评论
为什么被折叠?



