Java output console error message to file?

浪尽此生 提交于 2019-12-18 07:48:30

问题


I have a simple piece of code that outputs console text to a text file in Java:

PrintStream out = new PrintStream(new FileOutputStream("test2_output.txt"));
System.setOut(out);

However I require this text file to contain the error messages that is produced in the console but they are not included.

How do I do this?


回答1:


Add:

System.setErr(out);

at the end.




回答2:


There is also a System.setErr() call to redirect stderr.




回答3:


System.setErr(out)




回答4:


You're currently redirecting the standard output stream to a file. To redirect the standard error stream use

System.setErr(out);
  • System.setErr()javadoc



回答5:


Try:

PrintStream pst = new PrintStream("Text.txt");  
System.setOut(pst);
System.setErr(pst);
System.out.println("Hello, Finally I've printed output to file..");


来源:https://stackoverflow.com/questions/9270410/java-output-console-error-message-to-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!