0

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.

user2152962
  • 47
  • 2
  • 4
  • 1
    You're not showing any code that actually writes to a file. You may find [Basic I/O](http://docs.oracle.com/javase/tutorial/essential/io/) for some help – MadProgrammer May 13 '13 at 00:17
  • `FileDialog` might not necessarily be modal. In that case, `setVisible()` won't block, and your code proceeds to writing into a file before `getFile()` returns a sensible value. Generally, you shouldn't write GUI code "procedurally". Do work in appropriate event handlers. – millimoose May 13 '13 at 00:23
  • 2
    @millimoose From the Java Docs *"Since it is a modal dialog, when the application calls its show method to display the dialog, it blocks the rest of the application until the user has chosen a file."* – MadProgrammer May 13 '13 at 00:31
  • While I/O handling is slightly off, it look like it should write just fine. You may need to verify that `stuff` actually equals something (and it's valid) and that the `grades` are actually initialized and filled out...Either use a debugger or include `System.out.println` statements that you can use to track the flow of the application and the state of the variables... – MadProgrammer May 13 '13 at 00:35
  • I have tried that, and stuff printed as the exact file path, although I'm thinking that the fact the extension is part of it might be the problem? Not sure, will test that with JFileChooser now. Also, I know the grades exist, as I am able to write to a file that does not exist. It is only when trying to add to files that it does not work. – user2152962 May 13 '13 at 00:38
  • Fixed it: It did not like one of the if statements I had in my code. Not sure why, but no matter. It works now. – user2152962 May 13 '13 at 00:51

0 Answers0