Search code examples
javastreamconsolesystem

write console output in a file and console at the same time


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


Solution

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