9

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?

5 Answers 5

13

Add:

System.setErr(out);

at the end.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks very much. Did the job perfectly. I can't believe I couldn't find this statement anywhere on Google.
3

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

Comments

3

System.setErr(out)

Comments

2

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

System.setErr(out);

Comments

0

Try:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.