How to redirect console output to file using Java?

Java ConsoleIn Java printing the program output to console for debugging purposes and sometimes for understanding the program flow is very common. Sometimes programmers feel comfortable if they are able to redirect the run-time exceptions and SOPs (System.out.println) to a file for future reference. Today I am going to write about redirecting console outputs and run-time exceptions to a file. One can always argue that we can use logging frameworks like log4j, java util logging by writing custom OutputStream class and using SOPs (System.out.println) is a bad practice. I too agree with that but this is just trick for beginners to understand the basics of java and one should always avoid using SOPs while writing applications.

The above code sets the output stream of System class by creating an instance of PrintStream class with FileOutputStream as argument. This will redirect any console output using SOPs (System.out.println) to the file specified in the FileOutputStream. To redirect the runtime exceptions we need to use System.setErr. To restore the output back to cosole we are saving the stream before try to change it. In the above program I have intentionally thrown some exception to show and ensure that all runtime exceptions are captured and logged. Please do comment for any clarifications.

  • max

    How to use this class? example?

  • 0xff

    just copy the source code, compile it and run. Please use editors like Eclipse to run it if you dont have any java knowledge or google for how to run a java program. Thanks.

  • suchetana basu

    i am getting the result in the txt file as

    Redirecting output & exceptions to file
    java.lang.Exception: Test Exception
    at HtmlParseDemo.main(HtmlParseDemo.java:23)

    although my console consists of the results due to parsing an html page…

  • Michael

    Thanks for the great example! This was exactly what I was looking for…

  • Roshidah

    Dear respected Sir/Madam,

    I’ve tried using the example, thanks, it worked but can’t I view the “out.txt” file?

  • bella

    Unable to run this program. Package is out of bounds . Help out.

  • Mari

    Hello,

    Just add the code in the main class
    , just after the declaration of main method
    every line of your original code within the “try” block.

    PrintStream orgStream = null;
    PrintStream fileStream = null;
    try
    {
    // Saving the orginal stream
    orgStream = System.out;
    fileStream = new PrintStream(new FileOutputStream(“out.txt”,true));
    // Redirecting console output to file
    System.setOut(fileStream);
    // Redirecting runtime exceptions to file
    System.setErr(fileStream);

    // Here all your original code

    throw new Exception(“Test Exception”);

    }
    catch (FileNotFoundException fnfEx)
    {
    System.out.println(“Error in IO Redirection”);
    fnfEx.printStackTrace();
    }
    catch (Exception ex)
    {
    //Gets printed in the file
    System.out.println(“Redirecting output & exceptions to file”);
    ex.printStackTrace();
    }
    finally
    {
    //Restoring back to console
    System.setOut(orgStream);
    //Gets printed in the console
    System.out.println(“Redirecting file output back to console”);

    }

    Thank you very much, this code helped me a lot.

  • Rituparna

    I have done as commented by Mari. I am getting reuired output in out.txt file.All is Ok except that I want to view the same output in console also, that is output in both Console and out.txt file. But I am getting no output printed in console.