简体   繁体   中英

FileInputStream & FileOutputStream

I am trying to save objects from a Java application to a file for later use after the application is closed. I understand that you use FileInputStream/ObjectInputStream to read objects from a file and use FileOutputStream/ObjectOutputStream to write objects to a file. So far I am able to successfully save the objects to a file and reload them back into the application when I reopen the app. However my problem is that this only works once.

I start the application, input data and close the application. The data is written to the specified file and when I reopen the application the data is properly reloaded into the application, but if I add any new data it does not get written to the specified file when the app is closed. Then when I reopen the application a second time no data is read into the app leaving the app completely blank.

Here is my code for my object writing:

static class quit_Action implements ActionListener
{
    @Override
    public void actionPerformed (ActionEvent e)
    {
        try
        {
            FileOutputStream savedData = new FileOutputStream("groupData.txt", false);
            ObjectOutputStream save = new ObjectOutputStream(savedData);
            save.writeObject(existingGroups);
            save.writeObject(groupCombo);
            save.writeObject(groupList);
            System.out.println("Data Saved");
            save.close();
            System.out.println("savedData Closed");

        }
        catch(IOException ioe)
        {
        }
        System.exit(0);
    }
}

The first time this is run the system outputs "Data Saved" and "savedData Closed", but the second time this is run neither print statement is printed. Through troubleshooting I've determined all the code up to

ObjectOutputStream save = new ObjectOutputStream(savedData);

runs, but any lines after that do not occur except for the system exit.

Here is the code for my object reading:

try
    {
        FileInputStream loadedData = new FileInputStream("groupData.txt");
        ObjectInputStream load = new ObjectInputStream(loadedData);
        existingGroups = (ArrayList<NewGroup>) load.readObject();
        groupCombo = (DefaultComboBoxModel) load.readObject();
        groupList = (DefaultListModel) load.readObject();
        System.out.println("Data Loaded");
        loadedData.close();
        System.out.println("loadedData Closed");   
    }
    catch(IOException ioe2)
    {
    }

This has the same problem as the writing part in that "Data Loaded" and "loadedData Closed" are printed the first time but not the second time. The code up to

ObjectInputStream load = new ObjectInputStream(loadedData);

runs, but nothing after it inside the try statement.

If I delete the file groupData.txt and reopen the application I am able to read and write again, but only once. I have tried deleting the file in the application using the File.delete() method, but even if the application makes it past this line of code the file doesn't delete itself. My thought here was that I could delete the file then create a new one of the same name directly afterwards then writing the objects to the new file before closing the program. This has not worked. I have also tried creating a new file every time I create a FileOutPutStream using

FileOutputStream out = new FileOutputStream(new File(groupData.txt), false);

But this has not worked either.

I thought maybe I could come up with a way to create a file with a different name every time the application is opened and use that to create my streams, but I am not sure how I would do this and it would also clutter whatever directory the files are being created in with a bunch of useless files.

This is how my objects are initialized

static DefaultListModel groupList = new DefaultListModel();
static DefaultComboBoxModel groupCombo = new DefaultComboBoxModel();
static ArrayList<NewGroup> existingGroups = new ArrayList<NewGroup>();

I looked online, but am mostly only able to find tutorials of how to write/read objects to and from an application, which I have figured out how to do(though only once), which is why I figured I would ask for help. If this has already been asked I apologize, I looked through a dozen or so questions and they didn't pertain to this specific problem.

Instead of FileOutputStream savedData = new FileOutputStream("groupData.txt", ***false***); , use FileOutputStream savedData = new FileOutputStream("groupData.txt", ***true***); . The second parameter, when set to true, indicates that you want to add data to your file. As you chose 'false', existing data is erased before the new data is saved.

I'm a little late to the party, but for the future reference maybe...

You open savedData and save streams, but only close() the latter. FileOutputStream stays open and block your attempts to write to that file again.

Morale of the story: always use try-with-resources .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM