Reputation: 313
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
Reputation: 10038
You can append to the end of the file by passing true to the FileOutputStream constructor as in
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:
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
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
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