Transformation flow and object operation flow in java

Posted by OU_Student on Mon, 27 Sep 2021 10:38:51 +0200

Conversion flow:

InputStreamReader: decoding operation, byte → character, and the parent class is Reader

       —— Construction method  

InputStreamReader(InputStream in)Create an InputStreamReader object using the default character encoding
InputStreamReader(InputStream in,String chatset)Creates an InputStreamReader object using the specified character encoding

OutputStreamWriter: encoding operation, character → byte, and the parent class is Writer

         —— Construction method

OutputStreamWriter(OutputStream out)Creates an OutputStreamWriter object using the default character encoding
OutputStreamWriter(OutputStream out,String charset)Creates an OutputStreamWriter object using the specified character encoding

Object operation flow:

1. Object serialization stream

What?

Object serialization stream is to save objects to disk or transfer objects over the network.

Why?

After the byte sequence is written to the file, it is equivalent to persisting the information of an object in the file.

How?

This mechanism uses a byte sequence to represent an object, which contains information such as object type, object data and attributes stored in the object.

  • Writes the original data type and graph of the Java object to the OutputStream. You can use ObjectInputStream to read (refactor) objects. Persistent storage of objects can be achieved by using streaming files. If the stream is a network socket stream, you can refactor the object on another host or in another process

ObjectOutputStream:

Construction method:

ObjectOutputStream(OutputStream out)Creates an ObjectOutputStream that writes to the specified OutputStream

Method to serialize an object:

void writeObject(Object obj)Writes the specified object to ObjectOutputStream

2. Object deserialization stream

  • ObjectInputStream deserializes raw data and objects previously written using ObjectOutputStream

ObjectInputStream:

Construction method:

ObjectInputStream(InputStream in)Creates an ObjectInputStream read from the specified InputStream

Method to deserialize an object:

Object readObject()Read an object from ObjectInputStream

3. SerialVersionUID & transient [application]

serialVersionUID

  • After serializing an object with the object serialization stream, if we modify the class file to which the object belongs, will there be a problem reading the data?

    • There will be a problem and an InvalidClassException will be thrown

  • If something goes wrong, how to solve it?

    • Re serialization

    • Add a serialVersionUID to the class to which the object belongs

      • private static final long serialVersionUID = 42L;

transient

  • If the value of a member variable in an object does not want to be serialized, how to implement it?

    • The member variable is decorated with the transient keyword. The member variable marked with the keyword does not participate in the serialization process

4. Object operation flow exercise [application]

  • Case requirements

    Create multiple student class objects, write them to the file and read them into memory again

  • Implementation steps

    • Create serialized stream object

    • Create multiple student objects

    • Add student object to collection

    • Serialize collection objects into a file

    • Create deserialized stream object

    • Read the object data in the file into memory

  • code implementation

    Student class

public class Student implements Serializable{
    
    private static final long serialVersionUID = 2L;

    private String name;
    private int age;

    public Student() {
    }

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

Test class

public class Demo03 {
    /**
     *  read():
     *      Read to the end of the file. The return value is - 1
     *  readLine():
     *      Read to the end of the file and return the value null
     *  readObject():
     *      An exception is thrown directly when reading to the end of the file
     *  If there are multiple objects to be serialized, it is not recommended to directly serialize multiple objects into a file, because exceptions are easy to occur during deserialization
     *      Suggestion: store multiple objects to be serialized into a collection, and then serialize the collection into a file
     */
    public static void main(String[] args) throws Exception {
        /*// serialize
        //1.Create serialized stream object
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("myCode\\oos.txt"));
        ArrayList<Student> arrayList = new ArrayList<>();
        //2.Create multiple student objects
        Student s = new Student("Tong Liya ", 30);
        Student s01 = new Student("Tong Liya ", 30);
        //3.Add student object to collection
        arrayList.add(s);
        arrayList.add(s01);
        //4.Serialize collection objects into a file
        oos.writeObject(arrayList);
        oos.close();*/

        // Deserialization
      	//5. Create deserialization stream object
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("myCode\\oos.txt"));
      	//6. Read the object data in the file into memory
        Object obj = ois.readObject();
        ArrayList<Student> arrayList = (ArrayList<Student>)obj;
        ois.close();
        for (Student s : arrayList) {
            System.out.println(s.getName() + "," + s.getAge());
        }
    }
}

Topics: Java