Reputation: 51100
I am trying to write text content into a file using Java.
I am using a loop that wraps the following code:
Note: urlForAllStores
is a String object.
FileWriter fw = new FileWriter(Play.getFile(SHOPS_FILE),true);
//the true will append the new data
fw.write(urlForAllStores+System.getProperty("line.separator"));
//appends the string to the file
fw.close();
It works fine, but then when my loop finishes all the content is deleted, and I lose all the results.
Is there anything in here that suggests that would happen, or is it likely to be in some other part of the code.
Upvotes: 0
Views: 207
Reputation: 986
I have a file at /home/charlie/file.txt, containing Hey
$ cat /home/charlie/file.txt
gives
Hey
After running this code
try {
FileWriter fw=new FileWriter(new File("/home/charlie/file.txt"), true);
fw.write("This is written by Java \n");
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
(Because I don't have the class Play
, you should give that in your post)
And this command
$ cat /home/charlie/file.txt
My new output is
Hey
This is written by Java
So I can't reproduce your problem to find what's wrong.
Please let me know whether it works or not
Happy coding :) -Charlie
Upvotes: 2