I am new to Java and I am stuck at this part :
I am trying to output the console output into a text file using JAVA .
But the problem is I have a While loop running and my code writes the output to the file but deletes the previous one .
I want to append the while loop output to file .
Any help is appreciated . Thanks In advance 🙂
PrintStream out = new PrintStream(new FileOutputStream("output.txt"));
System.setOut(out);
You should perform the redirection once, and before you do anything else. i.e. before you enter your loop.
Note (also) that your shell can redirect to a file, and that might be preferable to changing the destination of
System.out. That’s feasible but perhaps unexpected for someone who’s going to debug your code in the future and wonder where their output is going…Or perhaps consider a logging framework ?