0

Im trying to learn how to use an Array List to write objects to a file, however, the file keeps being overwritten and the data is not being kept when I restart the program:

System.out.println("Enter DVD title: ");
String title = inputScanner.nextLine();
System.out.println("Enter main actor: ");
String actor = inputScanner.nextLine();
System.out.println("Enter DVD's release year:  ");
String yearOfRelease = inputScanner.nextLine();
newDVD = new DVD(title, actor, yearOfRelease);
System.out.println("Entered DVD details: ");
DVDCollection.add(newDVD);
writeDVDs(DVDCollection);
break;

Above code takes input from the user and stores it in an ArrayList.

When I restart the program it seems to wipe everything.

static void writeDVDs(ArrayList<DVD> DVDs) throws Exception {
    try {
        Boolean append = true;
        FileOutputStream fileOutputStream = new FileOutputStream("DVDCollection.dat", append);
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
        for (int i = 0; i < DVDs.size(); i++){
            DVD theDVD = DVDs.get(i);
            objectOutputStream.writeObject(theDVD);
            System.out.println("DVD successfully saved");
        }
    }
    catch (Exception e){
        System.out.println("Cant write data to file");
    }
}
3
  • I updated my answer again; and some feedback would be welcome ... Commented Apr 10, 2017 at 11:31
  • Just write the entire list. You don't need to iterate it. But you can't append to an object stream without taking special measures. Commented Apr 10, 2017 at 11:50
  • Thanks for the feedback, I didnt claim to be good at programming but this is def helping Commented Apr 10, 2017 at 15:52

1 Answer 1

2

No, it doesn't.

Your coding is missing the part of closing the FileOutputStream after writing to it. Depending on your context, this can leave things in a very inconsistent state.

And of course, with Java7, you should simply turn to try-with-resources to get your streams closed automatically! And hint: System.out.println("Cant write data to file") basically throws away all the details of the exact problem. You want to print the exception itself, too!

But beyond that, there is a conceptual problem. What your code is doing:

  • filling an array list object
  • opening a file for appending
  • writing the objects into that file ... one by one

That works; but isn't exactly great. You see; in order to read those values back - how do you know how many entries are within your file?

I would rather suggest to simply write the complete list object itself. But of course, that would require to rework the overall solution - for example like:

  • your method first reads the list currently stored in the file
  • then you add the new objects to that list
  • then you write that list to the file again (but in overwrite mode, not appending)

Long story short: you should probably step back and make up your mind what exactly your requirement is; and how to get there in clean way.

Sign up to request clarification or add additional context in comments.

2 Comments

Sorry, this is my first post, the reason for the project is to allow the user to create a list of their DVDs, store said list and edit the list. P.S. im a student just starting programming, i have less than 6 months experience
That isn't a problem. But when newbies ask questions it is typical that questions come back. It is thus not so great to drop a question here and walk away for hours...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.