tore
tore

Reputation: 313

Saving objects to a file, and adding more objects to it on a later occasion

I was thinking about making a little project that would help me track my diet. My first obstacle is building a library of food type-objects and writing them to a file.

I followed this guide http://beginwithjava.blogspot.no/2011/04/java-file-save-and-file-load-objects.html where they use

FileOutputStream saveFile=new FileOutputStream("SaveObj.sav");
ObjectOutputStream save = new ObjectOutputStream(saveFile); 
save.writeObject(foodType);

etc.

I have no problem writing and reading, but when I want to add more objects, I create a new saveFile and overwrite the existing one.

I've tried googling the matter, but I can't find any solutions to add more objects to an already existing file.

Also, is it best to add the objects to the file, or to save an ArrayList<foodObject> that I add the objects to?

Also, is .sav the best file format? Could I use .txt? I've seen .bin somewhere too. How do I choose?

Upvotes: 3

Views: 1149

Answers (4)

Brandon
Brandon

Reputation: 10038

You can append to the end of the file by passing true to the FileOutputStream constructor as in

http://docs.oracle.com/javase/6/docs/api/java/io/FileOutputStream.html#FileOutputStream(java.lang.String, boolean)

However, this poses an interesting question. When reading the file, how do you know how many objects were saved? To read in every object, you want to call readObject N times, once for each object, but you need to know what N is, otherwise you read past the end of the file and that is not good.

There are two solutions I can think of:

  1. Save a count of the number of objects to a second file, which you overwrite every time by reading the file, parsing to an int, adding 1, write back out. There is a risk of course that the files could get out of sync and then bad things happen.
  2. Save a collection (like ArrayList or HashSet) of objects and overwrite the file each time. This adds some more overhead, but in the grand scheme of things, is probably very little and not worth worrying about.

Also, is it best to add the objects to the file, or to save an ArrayList that I add the objects to?

Like all things in software, it depends. I gave you some pros and cons above to help you decide, but your milage may vary. Personally, I would use an ArrayList and just serialize that, but I don't have a complete picture of your needs, so I'm not going to say that it is "best".

Also, is .sav the best file format? Could I use .txt? I've seen .bin somewhere too. How do I choose?

There is no file format here. The format is the raw bytes from Java's object serialization. To read the file, you need a Java program with the same classes in order to deserialize the data structure. If you simply mean which is the best file extension, I don't know. It doesn't matter. .txt may mislead people into think it is a text file when it clearly is not, so I recommend not saving it as .txt.

.bin is acceptable seeing as the format is in fact binary. I recommend .bin over .sav because it's clearer. .save does not tell me anything other than data is saved there. It's no better than .data

Upvotes: 3

Evgeniy Dorofeev
Evgeniy Dorofeev

Reputation: 136062

To append to the file where you have already saved some objects you need something like this

class AppendingObjectOutputStream extends ObjectOutputStream {
    public AppendingObjectOutputStream(File file) throws IOException {
        super(new FileOutputStream(file, true));
    }

    protected void writeStreamHeader() throws IOException {
        reset();
    }
}

this class blocks an ObjectOutputStream header to be written.

BTW .sav is not a format but just a meaningless file name extenstion

Upvotes: 1

anshulkatta
anshulkatta

Reputation: 2064

Use JSON , it is best way to save Object.

Upvotes: 0

Sednus
Sednus

Reputation: 2113

 FileOutputStream saveFile = new FileOutputStream("SaveObj.sav", true);

According to the documentation:

Creates a file output stream to write to the file represented by the specified File object. If the second argument is true, then bytes will be written to the end of the file rather than the beginning.

Upvotes: 6

Related Questions