0

I'm wondering if I can let java write all console output in a txt file without disabling it on console

PrintStream o = new PrintStream(new File("A.txt"));
PrintStream console = System.out;
System.setOut(o);
System.setOut(console);

so I need both command be excecuted

  • 3
    Time to use a logging library? Take a look at this, as an example: https://stackoverflow.com/questions/3382985/how-to-make-log4j-to-write-to-the-console-as-well – ernest_k Dec 18 '19 at 11:00
  • The problem with log4j that I have to type logger.info every single time to log, I just want to do that automatically like stream, because my program is so huge – Abd Alhadi Jarad Dec 18 '19 at 11:22
  • This question is answered in [this answer](https://stackoverflow.com/a/1994721). Instead of using the custom `TeePrintStream` mentioned there you can also use Apache Commons' [TeeOutputStream](https://commons.apache.org/proper/commons-io/javadocs/api-2.4/org/apache/commons/io/output/TeeOutputStream.html). – Ivar Dec 18 '19 at 11:29
  • Do keep in mind though that if you don't have access to the file for some reason, you probably wont have any output at all. – Ivar Dec 18 '19 at 11:32

1 Answers1

0

I couldn't comment because of my reputation. But the answer below will solve your question : System.out to string

Volkan Albayrak
  • 329
  • 1
  • 11