I have a problem about loading/saving ArrayList from/to file.
I've searched some thread about this but it's not ok, because the demo used ArrayList<String> but i used ArrayList<Student> with Class Student
public class Student {
private String id;
private String name;
private int year;
private String genre;
private String course;
private String group;
private String falcuty;
public Student() {
}
public Student(String id, String name, int year, String genre, String course, String group, String falcuty) {
this.id = id;
this.name = name;
this.year = year;
this.genre = genre;
this.course = course;
this.group = group;
this.falcuty = falcuty;
}
public String getID() {
return id;
}
public void setID(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public String getGenre() {
return genre;
}
public void setGenre(String genre) {
this.genre = genre;
}
public String getCourse() {
return course;
}
public void setCourse(String course) {
this.course = course;
}
public String getGroup() {
return group;
}
public void setGroup(String group) {
this.group = group;
}
public String getFalcuty() {
return falcuty;
}
public void setFalcuty(String falcuty) {
this.falcuty = falcuty;
}
@Override
public String toString() {
return id +" "+ year + " " + genre + " " + course + " " + group + " " + falcuty;
}
}
when i used the code
private void saveToFile() {
try {
FileOutputStream fos = new FileOutputStream(new File("student.dat"));
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(listStudent);
oos.close();
} catch (FileNotFoundException ex) {
JOptionPane.showMessageDialog(this, ex);
System.out.print(ex);
} catch (IOException ex) {
JOptionPane.showMessageDialog(this, ex);
System.out.print(ex);
}
}
private void loadFromFile() {
try {
FileInputStream fis = new FileInputStream("student.dat");
ObjectInputStream ois = new ObjectInputStream(fis);
Object obj = ois.readObject();
ArrayList<Student> list = (ArrayList) obj;
tblInfomation.setModel(new TableModel(list));
} catch (FileNotFoundException ex) {
JOptionPane.showMessageDialog(this, ex);
} catch (IOException ex) {
JOptionPane.showMessageDialog(this, ex);
} catch (ClassNotFoundException ex) {
JOptionPane.showMessageDialog(this, ex);
}
}
this code to save/load ArrayList to file i have Exception
java.io.NotSerializableException: Data.Student --> problem of class Student
Someone can help me?
Thanks a lot!