I am having a problem with my code. I am using AWT FileDialog, and creating files work just fine. In my program, I have the option to add text to an existing file, but currently it does not add text to that file. I cannot figure out a solution, because it is not returning any errors. Here is my code:
if (optiondialog == 1) {
FileDialog fc = new FileDialog(form, "Choose a file to open", FileDialog.LOAD);
fc.setDirectory("~/");
fc.setVisible(true);
String fileName = fc.getFile();
if (!fileName.isEmpty()) {
stuff = new File(fc.getDirectory() + fc.getFile());
form.studentNameBox.requestFocus();
form.setVisible(true);
System.out.println(stuff);
noValidInput = false;
}
else {
JOptionPane.showMessageDialog(null, "ERROR! No name entered, please go back and try again.");
}
}
This is my method that writes to the file:
public static void addToFile() throws IOException {
FileWriter fw = new FileWriter(Main.stuff, true);
for (int i = 0; i < grades.size(); i++) {
fw.write(grades.get(i) + System.getProperty("line.separator"));
}
fw.close();
}
This code worked with a JFileChooser, but those windows came up ugly on macs. Like I said, I can write to a file if it didn't exist beforehand, but I cannot edit or add on to existing files, which wasn't an issue with my JFileChooser. Also, this method gets called when the window is closing (done with a WindowListener) or when a quit button is pushed on the JFrame I created.