Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing to console and text file

I found the code below from the internet, works but it doesn't write the printed console to omt.txt, it only writes the System.out.println statements after the second catch block.If you run the code once you will understand what I mean.All I want is to write what is on console to the "omt.txt" file that is all...

After some answers, I see that my question wasn't clear, sorry for that. I want to save console output to omt.txt text file. If on the console "Hello 123" is printed , it should be also in omt.txt file.In other words whatever on the console is printed should be simultaneously written on the om.txt file or can be after the console execution but should be 1-to-1 the same!

import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintStream;

public class Wrt_file {

    public static void main(String[] args) {
        System.out.println("THIS is what I see on the console. but not on TEXT file"); 

          File f = new File("omt.txt");
          if(!f.exists())
          {
            try {
                       f.createNewFile();
                } catch (Exception e) {
                    e.printStackTrace();
                }
          }

        try {
                FileOutputStream fos = new FileOutputStream(f);
                PrintStream ps = new PrintStream(fos);
                System.setOut(ps);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("THIS is what I see on the text file, but not on CONSOLE");      

        for (int i=0; i<10; i++){

            System.out.println("Testing");  
        }

    }

}
like image 514
Anarkie Avatar asked Apr 26 '13 13:04

Anarkie


People also ask

How do I save console WriteLine output to a text file?

WriteLine(sRes); SW. Close(); Console. WriteLine("File Created"); reader. Close();

What means console write?

While Write() and WriteLine() both are the Console Class methods. The only difference between the Write() and WriteLine() is that Console. Write is used to print data without printing the new line, while Console. WriteLine is used to print data along with printing the new line. Program 1: Example of Console.

How do I copy the console output to a text file in eclipse?

For that, go to Run -> Debug Configurations (or Run Configuration) on Eclipse menu. From the left panel pick your project. Go to Common tab. Then under "Standard Input and Output" section, click on checkbox next to "Output File:", and choose the name of output file to use.


2 Answers

Updated answer after learning that OP wants to duplicate streams

Since you want to write data in both streams try using TeeOutputStream from Apache Commons. Change your code in second try to

try {
    FileOutputStream fos = new FileOutputStream(f);
    Runtime.getRuntime().addShutdownHook(new Thread(() -> {
        try {
            fos.flush();
        }
        catch (Throwable t) {
            // Ignore
        }
    }, "Shutdown hook Thread flushing " + f));
    //we will want to print in standard "System.out" and in "file"
    TeeOutputStream myOut=new TeeOutputStream(System.out, fos);
    PrintStream ps = new PrintStream(myOut, true); //true - auto-flush after println
    System.setOut(ps);
} catch (Exception e) {
    e.printStackTrace();
}

Now results from System.out will also be placed in your file.

like image 160
Pshemo Avatar answered Nov 09 '22 05:11

Pshemo


The reason is :

The java.lang.System.setOut() method reassigns the "standard" output stream.

so when you use System.out.println it will print only in the text file

So , if you want to print on the text file and on the console , Try this :

   

    FileOutputStream fos = new FileOutputStream(f);
    PrintStream ps = new PrintStream(fos);
    ps.println("THIS is what I see on the text file, but not on CONSOLE");
    System.out.println("THIS is what I see on the text file, but not on CONSOLE");

        for (int i = 0; i < 4; i++) {

            ps.println("Testing");
            System.out.println("Testing");
        } 
like image 25
Alya'a Gamal Avatar answered Nov 09 '22 05:11

Alya'a Gamal