You are doing this:
- open file
- write number
- close file
- start over by going to (1).
This way, you are clearing the previous data of the file. Change your logic to:
- open file
- write number
- goto (2)
- when done, close file.
Alternatively, you could choose to write to the file by appending data. But that is not recommended in this scenario. If you would like to try it (only for educational purposes!), you could try to create your PrintWriters like this:
PrintWriter pw = new PrintWriter(new FileWriter(file, true));