I want create text file but if the file already exists it should not create new file but should append the text to the content (at the end) of the existing file. How can I d
If you wish to append to the file if it already exists there's no need to check for its existence at all using exists()
. You merely need to create a FileWriter
with the append flag set to true; e.g.
PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("foo.txt", true)));
pw.println("Hello, World");
pw.close();
This will create the file if it does not exist or else append to the end of it otherwise.