java sout写到文本文件里

PrintStream fileStream = new PrintStream("filename.txt");
System.setOut(fileStream);
以后所有的sout都变成记录日志了
然后一个小技巧
//这里开始写日志,启动日志记录
try{
Date cData = new Date(System.currentTimeMillis());
SimpleDateFormat ft = new SimpleDateFormat ("yyyy-MM-dd-hh-mm-ss");
String cDataStr = ft.format(cData);
PrintStream fileStream = new PrintStream("log_"+cDataStr+"_"+myProjectHelper.ProjectDest.substring(0, myProjectHelper.ProjectDest.length()-1)+".txt");
System.setOut(fileStream);
}catch (Exception e11){
e11.printStackTrace();
e11.getCause();
}
System.out.println("---------------------日志功能开启---------------------");
浙公网安备 33010602011771号