When we write data on a file, we usually make use of a loop structured as follows:
PrintWriter out = ...
while (condition){
out.println(data);
...}
out.close();
Note: The condition for terminating the loop does not depend on the file, but on the data which we are writing to the file.
Example: Method that writes an array of strings on a file. Both the array of strings and the filename are passed as parameters to the method.
import java.io.*;
public static void saveArray(String[] v, String filename) throws IOException {
FileWriter f = new FileWriter(filename);
PrintWriter out = new PrintWriter(f);
for (int i = 0; i < v.length; i++)
out.println(v[i]);
out.close();
f.close();
}