Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement Serializable?

How should I implement the Serializable interface?

I have a class Student, and need to be able to save it to disk. For my homework, I have to serialize five different Student objects and save them to file.

class Student {
     String mFirstName;
     String mSecondName;
     String mPhoneNumber;
     String mAddress;
     String mCity;

Student(final String pFirstName, final String pSecondName, final String pPhoneNumber, final String pAddress, final String pCity){
    this.mFirstName = pFirstName;
    this.mSecondName = pSecondName;
    this.mPhoneNumber = pPhoneNumber;
    this.mAddress = pAddress;
    this.mCity = pCity;

}}

I've tried using ObjectOutputStream to serialize a Student, but it throws an error:

ObjectOutputStream lOutputStream = new ObjectOutputStream(new FileOutputStream("file.txt", true));
lOutputStream.write(new Student("foo","bar","555-1234","Flat 40","Liverpool"));
like image 868
Mostafa M Awad Avatar asked Mar 01 '15 01:03

Mostafa M Awad


People also ask

How do you implement serialization?

For serializing the object, we call the writeObject() method of ObjectOutputStream class, and for deserialization we call the readObject() method of ObjectInputStream class. We must have to implement the Serializable interface for serializing the object.

How can we implement Serializable interface?

Serialization is done using ObjectOutputStream. Deserialization is the reverse process where the byte stream is used to recreate the actual Java object in memory. This mechanism is used to persist the object. Deserialization is done using ObjectInputStream.

What implements Serializable?

Button class implements the Serializable interface, so you can serialize a java. awt. Button object and store that serialized state in a file. Later, you can read back the serialized state and deserialize into a java.

How do you make Serializable?

To make a Java object serializable you implement the java. io. Serializable interface. This is only a marker interface which tells the Java platform that the object is serializable.


1 Answers

The only thing you need to do is implement Serializable. The only thing you need to worry when implementing this interface is to make sure that all fields of such class, has implemented Serializable interface as well. In your case all fields are Strings and they already implement Serializable. Therefore, you only need to add implements Serializable. https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/io/Serializable.html

public class Student implements Serializable {
    String first;
    String second;
    String phone;
    String cityAddress;
    String cityStreet;

    public Student(String s1, String s2, String s3, String s4, String s5) {
        first = s1;
        second = s2;
        phone = s3;
        cityAddress = s4;
        cityStreet = s5;
    }
}
like image 105
DonatasD Avatar answered Sep 22 '22 08:09

DonatasD