Message about Java console output to file?

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 error messages that are generated in the console, but they are not included.

How to do it?

+6
source share
5 answers

Add

 System.setErr(out); 

in the end.

+9
source

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

+2
source

You are currently redirecting standard output to a file. To redirect the standard error stream, use

 System.setErr(out); 
+2
source

Try:

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

Source: https://habr.com/ru/post/908388/


All Articles