Home >Java >javaTutorial >Java object serialization NIO NIO2 detailed introduction and analysis
This article mainly introduces the detailed introduction and analysis of Java object serialization NIO NIO2 related information. The serialization mechanism allows objects to exist independently of the running of the program. Friends in need can refer to it
Java object serialization NIO NIO2 detailed introduction and analysis
Summary:
Object serialization
The object serialization mechanism allows Java objects in memory to be converted into platform-independent binary streams, which can be saved to disk or transmitted over the network. After other programs obtain this binary stream, they can restore it to the original Java object. The serialization mechanism can enable objects to exist independently of the running of the program.
The meaning and meaning of serialization
Serialization
The serialization mechanism can Allowing objects to exist independently of the running of the program Restore the java object in the stream
If you need to make an object support the serialization mechanism, its class must be serializable. In order to make a class serializable, you must implement the following One of the two interfaces:
Implement Serializable class to implement serialization, The program can serialize the object through the following two steps:
1. Create an ObjectOutputStream. This output stream is a processing stream, so it must be built on the basis of other node streams
// 创建个ObjectOutputStream输出流 ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("object.txt"));
2. Call the writeObject method of the ObjectOutputStream object to output the serializable object
// 将一个Person对象输出到输出流中 oos.writeObject(per);
Define an NbaPlayer class, Implement the Serializable interface, which identifies that objects of this class are serializable
public class NbaPlayer implements java.io.Serializable { private String name; private int number; // 注意此处没有提供无参数的构造器! public NbaPlayer(String name, int number) { System.out.println("有参数的构造器"); this.name = name; this.number = number; } // name的setter和getter方法 public void setName(String name) { this.name = name; } public String getName() { return this.name; } // number的setter和getter方法 public void setNumber(int number) { this.number = number; } public int getNumber() { return this.number; } }
Use ObjectOutputStream to write an NbaPlayer object to a disk file
import java.io.*; public class WriteObject { public static void main(String[] args) { try( // 创建一个ObjectOutputStream输出流 ObjectOutputStream oos = new ObjectOutputStream( new FileOutputStream("object.txt"))) { NbaPlayer player = new NbaPlayer("维斯布鲁克", 0); // 将player对象写入输出流 oos.writeObject(player); } catch (IOException ex) { ex.printStackTrace(); } } }Deserialization
To restore Java objects from binary streams, you need to use deserialization. The program can pass the following two Steps to serialize the object:
1. Create an ObjectInputStream input stream. This input stream is a processing stream, so it must be built on the basis of other node streams
// 创建个ObjectInputStream输出流 ObjectInputStream ois = new ObjectInputStream(new FileInputStream("object.txt"));
2. Call the readObject() method of the ObjectInputStream object to read the object in the stream. This method returns a Java object of type Object, which can be cast to its real type
// 从输入流中读取一个Java对象,并将其强制类型转换为Person类 Person p = (Person)ois.readObject();
Steps to read NbaPlayer object from object.txt file
import java.io.*; public class ReadObject { public static void main(String[] args) { try( // 创建一个ObjectInputStream输入流 ObjectInputStream ois = new ObjectInputStream( new FileInputStream("object.txt"))) { // 从输入流中读取一个Java对象,并将其强制类型转换为NbaPlayer类 NbaPlayer player = (NbaPlayer)ois.readObject(); System.out.println("名字为:" + player.getName() + "\n号码为:" + player.getNumber()); } catch (Exception ex) { ex.printStackTrace(); } } }
Deserialization read Only the data of the Java object is obtained, not the Java class. Therefore, when using deserialization to restore the Java object, the class file to which the Java object belongs must be provided, otherwise a ClassNotFoundException exception will be thrown; the deserialization mechanism does not need to initialize Java through a constructor. Object
If multiple Java objects are written to the file using the serialization mechanism, the objects restored using the deserialization mechanism must be read in the order actually written. When a serializable class has multiple parent classes (including direct parent classes and indirect parent classes), these parent classes either have no-argument constructors or are also serializable - otherwise deserialization will throw an InvalidClassException. . If the parent class is not serializable and only has a parameterless constructor, the Field value defined by the parent class will not be serialized into the binary stream
Serialization of object referencesIf the Field type of a class is not a basic type or String type, but another reference type, then this reference type must be serializable, otherwise the class that uses this type of Field will not be able to Serialized
public class AllStar implements java.io.Serializable { private String name; private NbaPlayer player; public AllStar(String name, NbaPlayer player) { this.name = name; this.player = player; } // 此处省略了name和player的setter和getter方法 // name的setter和getter方法 public String getName() { return this.name; } public void setName(String name) { this.name = name; } // player的setter和getter方法 public NbaPlayer getPlayer() { return player; } public void setPlayer(NbaPlayer player) { this.player = player; } }Java special serialization algorithm
import java.io.*; public class WriteAllStar { public static void main(String[] args) { try( // 创建一个ObjectOutputStream输出流 ObjectOutputStream oos = new ObjectOutputStream( new FileOutputStream("allStar.txt"))) { NbaPlayer player = new NbaPlayer("詹姆斯哈登", 13); AllStar allStar1 = new AllStar("西部全明星", player); AllStar allStar2 = new AllStar("首发后卫", player); // 依次将四个对象写入输出流 oos.writeObject(allStar1); oos.writeObject(allStar2); oos.writeObject(player); oos.writeObject(allStar2); } catch (IOException ex) { ex.printStackTrace(); } } }
4 objects written to the output stream, In fact, only 3 are serialized, and the player references of the two AllStar objects in the sequence are actually the same NbaPlayer object. The following program reads objects in a serialized file
import java.io.*; public class ReadAllStar { public static void main(String[] args) { try( // 创建一个ObjectInputStream输出流 ObjectInputStream ois = new ObjectInputStream( new FileInputStream("allStar.txt"))) { // 依次读取ObjectInputStream输入流中的四个对象 AllStar star1 = (AllStar)ois.readObject(); AllStar star2 = (AllStar)ois.readObject(); NbaPlayer player = (NbaPlayer)ois.readObject(); AllStar star3 = (AllStar)ois.readObject(); // 输出true System.out.println("star1的player引用和player是否相同:" + (star1.getPlayer() == player)); // 输出true System.out.println("star2的player引用和player是否相同:" + (star2.getPlayer() == player)); // 输出true System.out.println("star2和star3是否是同一个对象:" + (star2 == star3)); } catch (Exception ex) { ex.printStackTrace(); } } }
If the same mutable Java object is serialized multiple times, only the first time it is serialized Will convert the Java object into a byte sequence and output
当使用Java序列化机制序列化可变对象时,只有第一次调用WriteObject()方法来输出对象时才会将对象转换成字节序列,并写入到ObjectOutputStream;即使在后面程序中,该对象的实例变量发生了改变,再次调用WriteObject()方法输出该对象时,改变后的实例变量也不会被输出
import java.io.*; public class SerializeMutable { public static void main(String[] args) { try( // 创建一个ObjectOutputStream输入流 ObjectOutputStream oos = new ObjectOutputStream( new FileOutputStream("mutable.txt")); // 创建一个ObjectInputStream输入流 ObjectInputStream ois = new ObjectInputStream( new FileInputStream("mutable.txt"))) { NbaPlayer player = new NbaPlayer("斯蒂芬库里", 30); // 系统会player对象转换字节序列并输出 oos.writeObject(player); // 改变per对象的name实例变量 player.setName("塞斯库里"); // 系统只是输出序列化编号,所以改变后的name不会被序列化 oos.writeObject(player); NbaPlayer player1 = (NbaPlayer)ois.readObject(); //① NbaPlayer player2 = (NbaPlayer)ois.readObject(); //② // 下面输出true,即反序列化后player1等于player2 System.out.println(player1 == player2); // 下面依然看到输出"斯蒂芬库里",即改变后的实例变量没有被序列化 System.out.println(player2.getName()); } catch (Exception ex) { ex.printStackTrace(); } } }
以上就是Java 对象序列化 NIO NIO2详细介绍及解析的内容,更多相关内容请关注PHP中文网(www.php.cn)!