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!

Recommended Answers

All 7 Replies

Java assumes that a class is not Serializsable (ie not able to be written to an Object stream) unless you explicitly say it is. To do that you declare the class as
implements Serializable
(check out the API for the Serializable interface)
If all your class data is just regular Strings and ints and so on, that's all you need to do.

commented: Help me java code +0

Thanks a lot! it worked! :D:D

OK, great, mark this thread "solved"?

OK, great, mark this thread "solved"?

How can i mark it??

You know what's strange? I have no idea how! I think it's something the originator (ie you) can do, but I can't. I guess there's a check box or something?

OK, so I know next time and look less of a dick, how did you do that?

There's a link under the last post:

Has this thread been answered?
If this thread has been successfully resolved, please Mark this Thread as Solved so that it can be added to our Knowledge Base and help others. Also, posters who replied appreciate knowing that they were helpful.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.