The problem is, that FileWriter writer = new FileWriter(filename) is overriding the file by default. So for each element the file is overridden.
I see 2 possible solutions:
Put the for loop inside the try() part. That way you override the file once only and write all elements before you close it.
You could use the constructor that takes filename and boolean value append.
And by the way: if you use the try (...) syntax, the close call is done automatically so your call of close is not required.
Konrad
No comments:
Post a Comment