And the stream of objects in Java Serialization

 

 

More recently, talking about the use of flow, the role of which is actually the stream of objects between the object and the self-defined classes of mutual conversion stream flow.

Still looks quite simple, then look at the following example:

public class Student{

private int id;

private String name;

private String sex;

private String tel;

 

// omitted getter, setter

 

public Student(int id, String name, String sex, String tel) {

super();

this.id = id;

this.name = name;

this.sex = sex;

this.tel = tel;

}

}

 

public class TestObjectStream {

public static void main(String[] args) {

writeObject();

readObject();

}

 

public static void writeObject() {

try {

FileOutputStream fos = new FileOutputStream("C:/Users/wangliang/Desktop/1");

ObjectOutputStream oos = new ObjectOutputStream(fos);

Student student1 = new Student(1, "Mary", "female", "11111");

Student student2 = new Student(2, "Jack", "male", "2222");

Student student3 = new Student(3, "Andy", "male", "3333");

List<Student> list = Arrays.asList(student1, student2, student3);

oos.writeObject (list); // write target

oos.flush ();

fos.close();

oos.close ();

} catch (Exception e) {

e.printStackTrace ();

}

}

 

public static void readObject() {

try {

FileInputStream fis = new FileInputStream("C:/Users/wangliang/Desktop/1");

ObjectInputStream ois = new ObjectInputStream(fis);

List <Student> list = (List <Student>) ois.readObject (); // object to be read

list.forEach(System.out::println);

fis.close();

ois.close();

} catch (Exception e) {

e.printStackTrace ();

}

}

}

 

Then during operation of abnormal findings are as follows:

java.io.NotSerializableException: com.qianfeng.day33.entity.Student

at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1184)

at java.io.ObjectOutputStream.writeArray(ObjectOutputStream.java:1378)

at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1174)

at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1548)

at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1509)

at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1432)

at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1178)

at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:348)

at com.qianfeng.day33.test.TestObjectStream.writeObject(TestObjectStream.java:28)

at com.qianfeng.day33.test.TestObjectStream.main(TestObjectStream.java:16)

 

According to this explanation abnormal tells us that no entity class to serialize, plus the sequence of code to try, as follows:

public class Student implements java.io.Serializable{

private static final long serialVersionUID = 4500674287188895027L;

 

private int id;

private String name;

private String sex;

private String tel;

 

// omitted getter, setter

 

public Student(int id, String name, String sex, String tel) {

super();

this.id = id;

this.name = name;

this.sex = sex;

this.tel = tel;

}

}

It is to achieve a java.io.Serializable interface, but look at the description of the interface source code:

*

* @author unascribed

* @see java.io.ObjectOutputStream

* @see java.io.ObjectInputStream

* @see java.io.ObjectOutput

* @see java.io.ObjectInput

* @see java.io.Externalizable

* @since JDK1.1

*/

public interface Serializable {

}

 

Found that the interface does not have any content, some of the operations described above in @see behind almost stream of objects, indicating serialized object stream is associated with the role, then the sequence of what in the end did it?

We can by way of analogy, to look at other language requirements for serialization, such as OC in this language requirements are as follows:

// Data (JSON data) ----> OC stream of objects

+ (id)JSONObjectWithData:(NSData *)data options:(NSJSONReadingOptions)opt error:(NSError **)error;

// OC objects -------> data stream (data Json)

+ (NSData *)dataWithJSONObject:(id)obj options:(NSJSONWritingOptions)opt error:(NSError **)error;

 

In OC, if one needs to implement serialization entity class, must implement these methods, the role of these two methods is to inform the system when it is desired to transfer an entity class or saved as a file as a stream, how to convert the into a JSON data (the specified key and value), then the string can be converted into a binary manner (byte array).

 

So in Java Why you do not need it? Because the Java reflection mechanism can easily complete this content.

So what is the sequence of the serial number of role is it? Is to ensure serialization and de-serialization can correctly only if the same serial number to a successful conversion.

 

Guess you like

Origin www.cnblogs.com/qfchen/p/11201743.html