Hi all,
I'm trying to write a program that holds student data in arraylists. I've managed to get all the adding, editing, deleting, displaying etc. working and now all I need to do is to make sure it saves on exit and loads on startup
By following another tutorial, I have managed to get the data saved into a file, but I can't get it to load back in again. I know there is something missing and I'm pretty sure it's obvious, but I'm having a total 'blonde moment' and can't think what it is!
Any help is appreciated, if you need more code, just say
Jamie
Save code:
private static void doSave() {
try{
FileOutputStream fos = new FileOutputStream("firstyears.dat");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(FirstYears);
oos.close();
fos.close();
}
catch (FileNotFoundException e){
e.printStackTrace();
}
catch (IOException e){
e.printStackTrace();
}
}
Load code:
private static void doLoad() {
try{
FileInputStream fis = new FileInputStream("firstyears.dat");
ObjectInputStream ois = new ObjectInputStream(fis);
Object obj = ois.readObject();
ArrayList<FirstYearStudent> FirstYears = (ArrayList<FirstYearStudent>) obj;
}
catch (FileNotFoundException e){
e.printStackTrace();
}
catch (IOException e){
e.printStackTrace();
}
catch (ClassNotFoundException e){
e.printStackTrace();
}
}
FirstYearStudent class:
package napier.coursework.sd3;
import java.io.Serializable;
public class FirstYearStudent implements Serializable{
private static final long serialVersionUID = 1L;
private String fname = null;
private String lname = null;
private String eats = null;
private String location = null;
public FirstYearStudent(String fname, String lname, String eats,
String location) {
this.setFname(fname);
this.lname = lname;
this.eats = eats;
this.location = location;
}
public String toString() {
return getFname() + " " + lname + " - " + eats + " - " + location;
}
public void setFname(String fname) {
this.fname = fname;
}
public String getFname() {
return fname;
}
public void setLname(String lname) {
this.lname = lname;
}
public String getLname() {
return lname;
}
public void setEats(String eats) {
this.eats = eats;
}
public String getEats() {
return eats;
}
public void setLocation(String location) {
this.location = location;
}
public String getLocation() {
return location;
}
}