Reputation: 314
New with Android. Sorry! I'm trying to create the following...
The code for writing to the file is as follows...
File file = new File("/sdcard/log.txt");
BufferedWriter writer = new BufferedWriter(new FileWriter(file));
writer.write(str);
writer.newLine();
writer.flush();
writer.close();
The code is only writing the last .write in the file! The previous WRITE is removed. I suspect that its creating a new file everytime and writing the latest line. I tried with FOS and OSW but with same results! Please help, am stuck on this one for almost 48 hours.
Upvotes: 1
Views: 2478
Reputation: 12092
You may want to use this constructor for FileWriter :
FileWriter (File file, boolean append)
By using the second argument as
true
then the bytes will be written to the end of the file rather than the beginning.
In your code use : BufferedWriter writer = new BufferedWriter(new FileWriter(file,true));
Upvotes: 2
Reputation: 376
Use the FileWriter
in append mode.
BufferedWriter writer = new BufferedWriter(new FileWriter(file, true));
Upvotes: 3