0

I am very new at programming so my apologies in advance if my question is a bit primary.

I have a piece of JAVA code (more than 10,000 lines) and it has hundreds of System.out.println("blabla"); and I need to save them in a .txt file. I cannot go through all of them and change the code so I was wondering if there is any way that I can add some lins of codes then run the program and finally save the logs when that Java program is finished.

  • Why can't you "go through all of them...?" You're not coding with pen and paper. Presumably you're using at least a text editor with find and replace functionality. Write yourself a little logging class that writes strings to a file, and wherever you're currently using System.out.println to dump log messages, call a method on your logging class instead. – MarsAtomic Aug 20 '15 at 04:36

4 Answers4

2

You can use this instruction somewhere at the beginning of your app:

System.setOut(new PrintStream("a.txt"));

UPDATE: If you want to write both console and file you can implement simple SplitStream:

public class SplitStream extends OutputStream {
    OutputStream o1,o2;
    public SplitStream(OutputStream o1, OutputStream o2) {
        this.o1=o1; this.o2=o2;
    }
    public void write(int b) throws IOException {
        o1.write(b);
        o2.write(b);
    }
}

then use it this way (in main):

OutputStream other=new FileOutputStream("a.txt");
System.setOut(new PrintStream(new SplitStream(System.out,other)));
krzydyn
  • 1,012
  • 9
  • 19
  • worked like a charm :) –  Aug 19 '15 at 18:55
  • The only issue is that it stopped showing the logs in my IDE (Eclipse) console :( –  Aug 19 '15 at 19:15
  • Yes, you can implement some condition depending how you run. Simplest is to add argument in eclipse run configuration. – krzydyn Aug 19 '15 at 19:20
  • could you explain a bit more, much appreciated –  Aug 19 '15 at 19:55
  • In Eclipse -> Run Configurations -> [you run config] -> Arguments add argument "logOnConsole", then in your main add condition: `if (args.length==0 || !args[0].equals("logOnConsole")) System.setOut(new PrintStream("a.txt"));` If your app needs command line arguments then you need add a little more logic to process all arguments – krzydyn Aug 19 '15 at 20:19
1

Change the "out" from System.out to print to an output file.

PrintStream out = new PrintStream(new FileOutputStream("output.txt"));
System.setOut(out);
Sterls
  • 723
  • 12
  • 22
  • The only issue is that it stopped showing the logs in my IDE (Eclipse) console :( –  Aug 19 '15 at 19:16
  • I thought you didn't care for it to be in the console anymore. Whilst it may be inefficient: At the end of the code, read the output.txt file and print its contents to the console. – Sterls Aug 19 '15 at 19:23
1

Maybe not exactly the answer to your question, but a solution without changing any line of code is to invoke the program and redirect the output to the text file

java your.Program > logfile.txt
Gerald Mücke
  • 10,724
  • 2
  • 50
  • 67
0

Since I want to write the data in both streams I try use TeeOutputStream from Apache Commons. Add the code at the start of you program.

try {
    FileOutputStream fos = new FileOutputStream(new File("BlaBla.txt"));
    //we will want to print in standard "System.out" and in "file"
    TeeOutputStream myOut=new TeeOutputStream(System.out, fos);
    PrintStream ps = new PrintStream(myOut);
    System.setOut(ps);
} catch (Exception e) {
    e.printStackTrace();
}

Thanks to Writing to console and text file

Community
  • 1
  • 1