public class ToLog {public static void main(String[] args) throws Exception {
//准备日志文件
File f = new File("log.txt");
if(!f.exists()){
f.createNewFile();
}//设置输出流
PrintStream out = System.out;
PrintStream ps = new PrintStream(f);
System.setOut(ps);//写入数据
String s = "This is a test sentence.";
System.out.println(s);//复原输出流
System.setOut(out);
ps.close();
}}
重定向输出流练习 ToLog,码迷,mamicode.com
时间: 2024-11-05 08:30:50