I write a simple function like this:
private static void write(String Swrite) throws IOException {
if(!file.exists()) {
file.createNewFile();
}
FileOutputStream fop=new FileOutputStream(file);
if(Swrite!=null)
fop.write(Swrite.getBytes());
fop.flush();
fop.close();
}
Every time I call it, it rewrite and then I just get the last items that are written. How can I change it to not rewriting? The file variable is defined globally as a File.
On your
FileOutputStreamconstructor, you need to add theboolean appendparameter. It will then look like this:This tells
FileOutputStreamthat it should append the file instead of clearing and rewriting all of its current data.