I want to be able to save the objects created in my program to a file. I have watched a few tutorials on ObjectOutputStream, but the problem is, they only show how to save a specific object created in the main method. What I want, I that the program automatically saves every created object. Taking the Group Object in my program as an example. This is the add method:
public void addGroup(int gid, String groupname) {
Group newgroup = new Group(gid, groupname);
if (!Groups.contains(newgroup)) {
Groups.add(newgroup);
return;
}else
JOptionPane.showMessageDialog(null, "Group with ID " + gid
+ " already exists!", "Error",
JOptionPane.WARNING_MESSAGE);
}
It is part of my database class. I wan to automatically save every created group to the file. How would this be done? where do I declare the new file, in the database class? in the main method?
My second question is, if I delete a group, using the remove method:
public void removeGroup(int gid) {
if (!Groups.remove(new Group(gid, null))) {
JOptionPane.showMessageDialog(null, "Group with ID[" + gid
+ "] not present. System unchanged.");
}
}
How do I delete it from the file? I know, that I cant really delete an Object from the file, but how will I blank out the space?
Thanks in advance for all the help :)