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 FileOutputStream
constructor, you need to add the boolean append
parameter. It will then look like this:
FileOutputStream fop = new FileOutputStream(file, true);
This tells FileOutputStream
that it should append the file instead of clearing and rewriting all of its current data.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With