I'm trying to save a file with JFileChooser
when clicking on a button.
So when I'm clicking on it, the window appear as I expect, then I put the file name and save it. All work, I get my file at the exact place and in .txt as I want, but when I open it, nothing in.
I've tested write and print but nothing works. So I would like to know where I'm wrong and how I should do.
Thanks !
Here is my code :
jbSave.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JFileChooser fileChooser = new JFileChooser();
if (fileChooser.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
try {
String path = file.getPath() + ".txt";
file = new File(path);
FileWriter filewriter = new FileWriter(file.getPath(), true);
BufferedWriter buff = new BufferedWriter(filewriter);
PrintWriter writer = new PrintWriter(buff);
writer.write("start");
} catch (FileNotFoundException e2) {
e2.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
});
The problem was that you did not close
PrintWriter
instance. You can resolve your problem by just closing thePrintWriter
after you finish writing like this :