I have this method:
public void save(String pName) throws IOException {
FileChooser choiceFile = new FileChooser();
choiceFile.setInitialDirectory(new File(System
.getProperty("user.dir")));
Window window = null;
File file = choiceFile.showSaveDialog(window);
PrintWriter writeLine = new PrintWriter(new FileWriter(file, true));
FileOutputStream fosTemp = null;
BufferedOutputStream bosTemp = null;
String data = "";
try {
fosTemp = new FileOutputStream(file);
bosTemp = new BufferedOutputStream(fosTemp);
try {
data += "A new line of text\n";
writeLine.println(data);
writeLine.flush();
writeLine.close();
bosTemp.flush();
bosTemp.close();
} catch (FileNotFoundException e) {
System.out.println("ERROR opening");
} catch (IOException e) {
System.out.println("ERROR closing");
}finally {
}}finally{
}
}
I want to add new lines of text to a file.
For that, I must open the save dialog window.
Then I must select the file (for instance --> test.txt) and click on the save button.
Then it must add the new line of text to the end of the 'test.txt' file.
But my problem is that it always tells me that the file already exist. Then it asks me if I want to overwrite it. If i say no, it doesn't want to save. If I say yes, it overwrites the 'test.txt' file with a new file called 'test.txt' with the new line of text inside.
How can I make it so it doesn't asks me to overwrite 'test.txt' when I select it and then click save so it just add the new line of text inside the 'test.txt' file?
I must also specify that I'm new to reading and writing files with java. So if I made a lot of mistakes regarding code complexity, etc. just tell me.
I already search on a lot of forums and different threads on this very website. I found some tips, but they don't fix my problem.
Thanks for answers.