Вопрос

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

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.

Другие советы

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

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top