Is it possible to open a file a in java append data and close numerous times. For example:
//---- psuedocode
//class variable declaration
FileWriter writer1 = new FileWriter(filename);
fn1:
writer.append(data - title);
fn2:
while(incomingdata == true){
writer.append(data)
writer.flush();
writer.close()
}
The problem lies in the while loop. The file is closed and cant be opened again. Any one can help me in this?
解决方案
The answers that advise against closing and re-opening the file each time are quite correct.
However, if you absolutely have to do this (and it's not clear to me that you do), then you can create a new FileWriter each time. Pass true as the second argument when you construct a FileWriter, to get one that appends to the file instead of replacing it. Like
FileWriter writer1 = new FileWriter(filename, true);