Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write console output to a txt file

I have tried to write the console output to a txt file using this code suggestion (http://www.daniweb.com/forums/thread23883.html#) however I was not successful. What's wrong?

try {       //create a buffered reader that connects to the console, we use it so we can read lines       BufferedReader in = new BufferedReader(new InputStreamReader(System.in));        //read a line from the console       String lineFromInput = in.readLine();        //create an print writer for writing to a file       PrintWriter out = new PrintWriter(new FileWriter("output.txt"));        //output to the file a line       out.println(lineFromInput);        //close the file (VERY IMPORTANT!)       out.close();    }       catch(IOException e1) {         System.out.println("Error during reading/writing");    } 
like image 517
Jessy Avatar asked Jan 03 '10 07:01

Jessy


People also ask

How do you save a console output to a text file in R studio?

You can also save the entire R console screen within the GUI by clicking on "Save to File..." under the menu "File." This saves the commands and the output to a text file, exactly as you see them on the screen.

How do I pipe a command to a text file?

Right-click the top result and select the Run as administrator option. Type the following command to save the output to a text file and press Enter: YOUR-COMMAND | Out-File -FilePath C:\PATH\TO\FOLDER\OUTPUT. txt.

How do I save output in Java?

Instantiate a PrintStream class by passing the above created File object as a parameter. Invoke the out() method of the System class, pass the PrintStream object to it. Finally, print data using the println() method, and it will be redirected to the file represented by the File object created in the first step.


2 Answers

You need to do something like this:

PrintStream out = new PrintStream(new FileOutputStream("output.txt")); System.setOut(out); 

The second statement is the key. It changes the value of the supposedly "final" System.out attribute to be the supplied PrintStream value.

There are analogous methods (setIn and setErr) for changing the standard input and error streams; refer to the java.lang.System javadocs for details.

A more general version of the above is this:

PrintStream out = new PrintStream(         new FileOutputStream("output.txt", append), autoFlush); System.setOut(out); 

If append is true, the stream will append to an existing file instead of truncating it. If autoflush is true, the output buffer will be flushed whenever a byte array is written, one of the println methods is called, or a \n is written.


I'd just like to add that it is usually a better idea to use a logging subsystem like Log4j, Logback or the standard Java java.util.logging subsystem. These offer fine-grained logging control via runtime configuration files, support for rolling log files, feeds to system logging, and so on.

Alternatively, if you are not "logging" then consider the following:

  • With typical shells, you can redirecting standard output (or standard error) to a file on the command line; e.g.

    $ java MyApp > output.txt    

    For more information, refer to a shell tutorial or manual entry.

  • You could change your application to use an out stream passed as a method parameter or via a singleton or dependency injection rather than writing to System.out.

Changing System.out may cause nasty surprises for other code in your JVM that is not expecting this to happen. (A properly designed Java library will avoid depending on System.out and System.err, but you could be unlucky.)

like image 70
Stephen C Avatar answered Sep 18 '22 16:09

Stephen C


There is no need to write any code, just in cmd on the console you can write:

javac myFile.java java ClassName > a.txt 

The output data is stored in the a.txt file.

like image 30
ali ahmad Avatar answered Sep 20 '22 16:09

ali ahmad