How to Write Console Output to File using Java

[9448 views]




java.lang.System out and err objects are utilized to write textual data to standard output stream and standard error stream respectively as the default output stream is Command line Console. But System class likewise gives other methods to redirect the standard output stream and standard error stream to some other destination such as file stream. You can use this to log console data to a log file directly in your java program.

java.lang.System Output Stream Methods

  1. setOut(PrintStream ps)
    The java.lang.System.setOut() method is used to reassign the "standard" output stream.
    Syntax:
    public static void setOut(PrintStream out)
  2. setErr(PrintStream ps)
    The java.lang.System.setErr() method is used to reassign the "standard" error output stream.
    Syntax:
    public static void setErr(PrintStream err)

Java Program to Write Command line Output to File

import java.io.FileNotFoundException; import java.io.PrintStream; import java.util.Scanner; class ConsoleToFile { public static void main(String[] args) { try { // Store current System.out before assigning a new value PrintStream console = System.out; // Creating file output stream PrintStream fileOut = new PrintStream("./out.txt"); System.out.println("Enter data line by line. Type exit to terminate the program"); // Redirecting standard console output to a file. System.setOut(fileOut); // Get input using Scanner from Console Scanner scanner = new Scanner(System.in); // Read string line. // Write exit to terminate the program String inputLine = scanner.nextLine(); while(true) { // If user input 'quit' then break the loop. if("exit".equalsIgnoreCase(inputLine)) { break; } System.out.println(inputLine); // Get next user input line text. inputLine = scanner.nextLine(); } System.out.println("Program terminated "); // Use stored value for output stream System.setOut(console); System.out.println("Program terminated"); }catch(FileNotFoundException ex) { ex.printStackTrace(); } } }

Console View:

C:\Users\SK\Desktop>java ConsoleToFile Enter data line by line. Type exit to terminate the program thank you for visiting solutionfactory.in Here you get posts on programming and related Technology. you can also contact us for any technical needs exit Program terminated

Output

After executing above code, you can see out.txt file generated. Content of out.txt file looks like this

Write Console text directly to a file using java

Java Program to Write Command line Error to a File

import java.io.FileNotFoundException; import java.io.PrintStream; import java.util.Scanner; public class ConsoleToFileError { public static void main(String[] args) { try { // Save original err stream. PrintStream originalErr = System.err; // Creating a new file error stream. PrintStream fileErr = new PrintStream("./error.txt"); System.out.println("Enter data line by line. Type exit to terminate the program"); // Redirect standard err to file. System.setErr(fileErr); // Get user data from scanner using console Scanner scanner = new Scanner(System.in); // Read string line // Write exit to terminate the program String inputLine = scanner.nextLine(); while(true) { // If user input 'exit' then break the loop if("exit".equalsIgnoreCase(inputLine)) { break; } System.err.println(inputLine); // Get next user input line text. inputLine = scanner.nextLine(); } System.err.println("Program Terminated. "); // set original error stream back again. System.setErr(originalErr); System.err.println("Program Terminated. "); }catch(FileNotFoundException ex) { ex.printStackTrace(); } } }

Console View:

C:\Users\SK\Desktop>javac ConsoleToFileError.java C:\Users\SolutionFactory\Desktop>java ConsoleToFileError Enter data line by line. Type exit to terminate the program This line will be printed in a file using System.err System.err by default prints in Console exit Program Terminated.

Output

After executing above code, you can see error.txt file generated. Content of error.txt file looks like this

Write Console Error directly to a file using java
                 



Clear Any Java Interview by Reading our Ebook Once.


Can you clear Java Interview?




Comments










Search Anything:

Sponsored Deals ends in





Technical Quizzes Specially For You:

Search Tags

    write java.lang.system out to a file in java

    Move Console Output to File in java