Persisting multiple objects of the same type to a file.
Let's say I have a class called "Names", and it has two properties: firstName and lastName.
And let's say I want to persist the data to a file.
And let's assume that the first time the firstName is "John" and last name is "Doe". I want to persist that to a file (say, names.txt, for instance).
But what I'd like to be able to do, also, is persist more names, say for instance "Tom Smith", "Bob Jones", and "Tim Timson".
Is there a way to do that?
I've figured out how to persist one object to a file (using serialization), but I can't figure out how to do multiple ones.
Advice?
Re: Persisting multiple objects of the same type to a file.
Have you tried writing out more than one object to a file?
Re: Persisting multiple objects of the same type to a file.
Quote:
Originally Posted by
Norm
Have you tried writing out more than one object to a file?
Good question. Yes, I have. I neglected to mention that.
When I try to do that, the only thing that gets written to the file is the final object. I'm assuming what actually happens is that it writes each object, but overwrites that object with the next one.
Re: Persisting multiple objects of the same type to a file.
Please post the code that shows the problem.
How are you writing to the file? Do you close it between writes? Do you specify to append data to the file when opening it for output?
Re: Persisting multiple objects of the same type to a file.
Quote:
Originally Posted by
Norm
Please post the code that shows the problem.
How are you writing to the file? Do you close it between writes? Do you specify to append data to the file when opening it for output?
Norm:
Thanks for asking. Before I post the code, yes, I understand that I shouldn't "hard code" paths to files (i.e. C:\MyFiles\somefile.txt), because of the various file systems on various machines.
The code below is executed when a button is clicked. I want to be able to change the values, click the button, and have the new value appended to the file. I'll figure out how to read the values back out later.
Anyway, here's the code.
Code :
btnSave.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
ObjectOutputStream oos = null;
try {
oos = new ObjectOutputStream(new FileOutputStream("C:\\PersistedData.txt"));
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
oos.writeObject(new String(lblGeneratedPwd.toString()));
oos.writeChars(lblGeneratedPwd.toString());
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
oos.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
Thanks in advance for your efforts.
Re: Persisting multiple objects of the same type to a file.
Quote:
have the new value appended to the file
Did you research this question:
Quote:
Do you specify to append data to the file when opening it for output?
Read the API doc for the FileOutputStream class.
Re: Persisting multiple objects of the same type to a file.
Quote:
Originally Posted by
Norm
Did you research this question:
Read the API doc for the FileOutputStream class.
Exactly what I needed. Should have thought about this myself. Thanks headed your way.