2

I'm working on a program about students (saving the name, age and other information into objects and storing those objects into an arrayList). So far so good, I even got the JFrames to work exactly how I want them but the problem shows up when I try to write all that information in a text file.

How can I do that?

I tried the FileOutputStream with the ObjectOutputStream but I'm most likely not doing it right since I keep getting an IOException.

An example of a program storing an arrayList with 2 or more Objects containing 2 or more variables each is pretty much what I'm looking for.

As for the loading of that saved array, I think I can figure it out but an explaination on that would still be apreciated.

I've seen a couple of times on different websites people suggesting to do (but it gives me an IOException)

    FileOutputStream fos=new FileOutputStream("FileName.txt");
    ObjectOutputStream oos=new ObjectOutputStream(fos);

    oos.writeObject(myObjectArrayList);

2 Answers2

3

By default ArrayList is Serializable.

But the Objects inside the list also must implement Serializable interface.

While writing make sure that your Student class implemented the java.io.Serializable

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
0

While serializing your ArrayList to a file you need to make sure the object that list contains are also serializable. So make sure your Student class implements Serializable interface.

Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
  • It clearly states that I'm supposed to avoid this sort of thing but... Thanks a lot I've been stuck on this all day. – Java Learer Aug 31 '13 at 14:31