Java redirect system.out to file

How to redirect console output and runtime exceptions to a file in Java

In Java printing the program output to console for debugging purposes and sometimes for understanding the program flow is very common. One can always argue that we can use logging frameworks like Apache log4j. I agree with that but sometimes the program is just a standalone process not part of a big application. And we want something quick and dirty for logging that info to a file.

Here is how to do it

Add the following code to the beginning of the program call.
PrintStream fileStream = new PrintStream(new FileOutputStream("myfile.txt",true));

//Redirecting console output to file (System.out.println)

//Redirecting runtime exceptions to file

No comments:

Post a Comment

NO JUNK, Please try to keep this clean and related to the topic at hand.
Comments are for users to ask questions, collaborate or improve on existing.