Hi all,
I'm new here and a little flummoxed by this problem. I am working through the process of creating a notepad type program in java to get a little more experience with the language. My save functionality is not working. This is not unexpected; the part that is really throwing me for a loop is that I am not getting any exception or indication that it doesn't work other than the fact that the file in question is empty. The pertinent code:
public void saveTheFile() {
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new File("C:\\workspace\\Utilities\\bin\\xmlEditor\\xml"));
chooser.setFileFilter(new javax.swing.filechooser.FileFilter() {
public boolean accept(File f) {
return f.getName().toLowerCase().endsWith(".xml")
|| f.isDirectory();
}
public String getDescription() {
return "XML Files";
}
});
if (chooser.showSaveDialog(new JFrame()) == JFileChooser.APPROVE_OPTION) {
fileName = chooser.getSelectedFile().getName();
try {
chooser.getSelectedFile().createNewFile();
BufferedWriter outgoing = new BufferedWriter(new FileWriter(fileName));
System.out.println(chooser.getSelectedFile().canWrite());
System.out.println(contents.toString());
outgoing.write(contents.toString());
outgoing.close();
} catch (Exception e){
e.printStackTrace();
}
}
}
My System.out.println
statements on 20 and 21 are giving me the output I would expect - namely true and the contents of the file. contents
is a StringBuilder
, btw.