Dan_Dan_Man
Dan_Dan_Man

Reputation: 504

Java output console error message 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 the error messages that is produced in the console but they are not included.

How do I do this?

Upvotes: 9

Views: 13921

Answers (5)

Kunal Bodke
Kunal Bodke

Reputation: 21

Try:

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

Upvotes: 0

calebds
calebds

Reputation: 26228

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

System.setErr(out);

Upvotes: 2

Jeffrey
Jeffrey

Reputation: 44808

System.setErr(out)

Upvotes: 3

icyrock.com
icyrock.com

Reputation: 28608

Add:

System.setErr(out);

at the end.

Upvotes: 13

colbadhombre
colbadhombre

Reputation: 813

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

Upvotes: 3

Related Questions