Java – how to attach written data to a file?
•
Java
I'm Android I's new developer wants to write some files. I've used a method to write files, as shown below
public void writeFile(String path,String text){ try{ Writer output = null; File file = new File(path); output = new BufferedWriter(new FileWriter(file)); output.write(text); output.close(); System.out.println("Your file has been written"); } catch (Exception e) { e.printStackTrace(); }
Here I pass a file and text path to write If I use this method, I can write data, but the previous data is being lost
How do I attach or insert the latest text into a file without losing previous text?
Thank you in advance
Solution
Try this Change this business
output = new BufferedWriter(new FileWriter(file));
to
output = new BufferedWriter(new FileWriter(file,true));
True means you want to append without overwriting
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码