Try writing to an output stream (not a redirected System.out
).
With FileOutputStreams
you can select if you want to append to a file or write a new file (the boolean in the constructor, have a look at the JavaDoc).
Try this code to create an output stream to a file which will not overwrite the file, but append to it.
OutputStream out = new FileOutputStream(new File("Test.txt"), true);
Also make sure you do not create the Stream in every iteration of your loop, but at the start of the loop.
If you then also close the output stream after the loop (in a finally block), then you should be ok.