This might have already been asked before, but I haven't found a solution that quite fits my situation.
I have a program that repeatedly prints questions to the user and waits for user input (all in console). I don't clear the screen after each question/response, so the result is all of the questions and the user's answers just sitting there in the console.
Now my question is how do I take whatever is in the console at that current moment and save it into a text file? The way that my program is currently setup makes it illogical to individually save all of my prints and the user's scanner inputs into a text file.
Is there a way to simply read whatever is in the console at that moment and save it into a text file?
CodePudding user response:
Instead of directly calling the System.out.print
methods, you can create your own print
method that both outputs to the console and stores what was output into a buffer. Then, you can directly write the buffer to a file when necessary.
Alternatively, System.setOut
can be used in conjunction with Apache Commons TeeOutputStream
.