所谓对象序列化就是将对象的状态转换成字节流,以后可以通过这些值再生成相同状态的对象。这个过程也可以通过网络实现,可以先在Windows机器上创建一个对象,对其序列化,然后通过网络发给一台Unix机器,然后在那里准确无误地重新"装配"。像RMI、Socket、JMS、EJB它们中的一种,彼此为什么能够传递Java对象,当然都是对象序列化机制的功劳。
Java对象序列化机制一般来讲有两种用途:
Java的JavaBeans: Bean的状态信息通常是在设计时配置的,Bean的状态信息必须被存起来,以便当程序运行时能恢复这些状态信息,这需要将对象的状态保存到文件中,而后能够通过读入对象状态来重新构造对象,恢复程序状态。
RMI允许象在本机上一样操作远程机器上的对象;或使用套接字在网络上传送对象的程序来说,这些都是需要实现serializaiton机制的。
我们通过让类实现Java.io.Serializable 接口可以将类序列化。这个接口是一个制造者(marker)接口。也就是说,对于要实现它的类来说,该接口不需要实现任何方法。它主要用来通知Java虚拟机(JVM),需要将一个对象序列化。
对于这个,有几点我们需要明确:
并非所有类都可以序列化,在cmd下,我们输入serialver Java.net.Socket,可以得到socket是否可序列化的信息,实际上socket是不可序列化的。
Java有很多基础类已经实现了serializable接口,比如string,vector等。但是比如hashtable就没有实现serializable接口。
将对象读出或者写入流的主要类有两个: ObjectOutputStream与ObjectInputStream .ObjectOutputStream 提供用来将对象写入输出流的writeObject方法, ObjectInputStream提供从输入流中读出对象的readObject方法。使用这些方法的对象必须已经被序列化的。也就是说,必须已经实现 Serializable接口。如果你想writeobject一个hashtable对象,那么,会得到一个异常。
序列化的过程就是对象写入字节流和从字节流中读取对象。将对象状态转换成字节流之后,可以用Java.io包中的各种字节流类将其保存到文件中,管道到另一线程中或通过网络连接将对象数据发送到另一主机。对象序列化功能非常简单、强大,在RMI、Socket、JMS、EJB都有应用。对象序列化问题在网络编程中并不是最激动人心的课题,但却相当重要,具有许多实用意义。
对象序列化可以实现分布式对象。主要应用例如:RMI要利用对象序列化运行远程主机上的服务,就像在本地机上运行对象时一样。
Java对象序列化不仅保留一个对象的数据,而且递归保存对象引用的每个对象的数据。可以将整个对象层次写入字节流中,可以保存在文件中或在网络连接上传递。利用对象序列化可以进行对象的“深复制”,即复制对象本身及引用的对象本身。序列化一个对象可能得到整个对象序列。
Java序列化比较简单,通常不需要编写保存和恢复对象状态的定制代码。实现Java.io.Serializable接口的类对象可以转换成字节流或从字节流恢复,不需要在类中增加任何代码。只有极少数情况下才需要定制代码保存或恢复对象状态。这里要注意:不是每个类都可序列化,有些类是不能序列化的,例如涉及线程的类与特定JVM有非常复杂的关系。
序列化机制:
序列化分为两大部分:序列化 和反序列化 。序列化是这个过程的第一部分,将数据分解成字节流,以便存储在文件中或在网络上传输。反序列化就是打开字节流并重构对象。对象序列化不仅要将基本数据类型转换成字节表示,有时还要恢复数据。恢复数据要求有恢复数据的对象实例。ObjectOutputStream中的序列化过程与字节流连接,包括对象类型和版本信息。反序列化时,JVM用头信息生成对象实例,然后将对象字节流中的数据复制到对象数据成员中。下面我们分两大部分来阐述:
处理对象流:
(序列化过程和反序列化过程)
Java.io包有两个序列化对象的类。ObjectOutputStream负责将对象写入字节流,ObjectInputStream从字节流重构对象。
我们先了解ObjectOutputStream类吧。ObjectOutputStream类扩展DataOutput接口。
writeObject() 方法是最重要的方法,用于对象序列化。如果对象包含其他对象的引用,则writeObject()方法递归序列化这些对象。每个 ObjectOutputStream维护序列化的对象引用表,防止发送同一对象的多个拷贝。(这点很重要)由于writeObject()可以序列化整组交叉引用的对象,因此同一ObjectOutputStream实例可能不小心被请求序列化同一对象。这时,进行反引用序列化,而不是再次写入对象字节流。
下面,让我们从例子中来了解ObjectOutputStream这个类吧。
// 序列化 today's date 到一个文件中.
FileOutputStream f = new FileOutputStream ("tmp" );
ObjectOutputStream s = new ObjectOutputStream (f);
s.writeObject("Today" );
s.writeObject(new Date ());
s.flush();
现在,让我们来了解ObjectInputStream这个类。它与ObjectOutputStream相似。它扩展DataInput接口。 ObjectInputStream中的方法镜像DataInputStream中读取Java基本数据类型的公开方法。readObject()方法从字节流中反序列化对象。每次调用readObject()方法都返回流中下一个Object。对象字节流并不传输类的字节码,而是包括类名及其签名。 readObject()收到对象时,JVM装入头中指定的类。如果找不到这个类,则readObject()抛出 ClassNotFoundException,如果需要传输对象数据和字节码,则可以用RMI框架。ObjectInputStream的其余方法用于定制反序列化过程。
例子如下:
//从文件中反序列化 string 对象和 date 对象
FileInputStream in = new FileInputStream ("tmp" );
ObjectInputStream s = new ObjectInputStream (in);
String today = (String )s.readObject();
Date date = (Date )s.readObject();
定制序列化过程:
序列化通常可以自动完成,但有时可能要对这个过程进行控制。java可以将类声明为serializable,但仍可手工控制声明为static或transient的数据成员。
例子:一个非常简单的序列化类。
public class simpleSerializableClass implements Serializable {String sToday="Today:" ;
transient Date dtToday=new Date ();
}
序列化时,类的所有数据成员应可序列化除了声明为transient 或static的成员。将变量声明为transient告诉JVM我们会负责将变元序列化。将数据成员声明为transient后,序列化过程就无法将其加进对象字节流中,没有从transient数据成员发送的数据。后面数据反序列化时,要重建数据成员(因为它是类定义的一部分),但不包含任何数据,因为这个数据成员不向流中写入任何数据。记住,对象流不序列化static或transient。我们的类要用writeObject()与 readObject()方法以处理这些数据成员。使用writeObject()与readObject()方法时,还要注意按写入的顺序读取这些数据成员。
关于如何使用定制序列化的部分代码如下
//重写writeObject()方法以便处理transient的成员。
public void writeObject(ObjectOutputStream outputStream) throws IOException {
outputStream.defaultWriteObject();//使定制的writeObject()方法可以
利用自动序列化中内置的逻辑。
outputStream.writeObject(oSocket.getInetAddress());
outputStream.writeInt(oSocket.getPort());
}
//重写readObject()方法以便接收transient的成员。
private void readObject(ObjectInputStream inputStream) throws IOException ,
ClassNotFoundException {
inputStream.defaultReadObject();//defaultReadObject()补充自动序列化
InetAddress oAddress=(InetAddress )inputStream.readObject();
int iPort =inputStream.readInt();
oSocket = new Socket (oAddress,iPort);
iID=getID();
dtToday =new Date ();
}
完全定制序列化过程:如果一个类要完全负责自己的序列化,则实现Externalizable接口而不是Serializable接口。Externalizable接口定义包括两个方法writeExternal()与readExternal()。利用这些方法可以控制对象数据成员如何写入字节流.类实现 Externalizable时,头写入对象流中,然后类完全负责序列化和恢复数据成员,除了头以外,根本没有自动序列化。这里要注意了。声明类实现 Externalizable接口会有重大的安全风险。writeExternal()与readExternal()方法声明为public,恶意类可以用这些方法读取和写入对象数据。如果对象包含敏感信息,则要格外小心。这包括使用安全套接或加密整个字节流。到此为至,我们学习了序列化的基础部分知识。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 | [size=1em]<code [size=1em]class[size=1em]=[size=1em]"hljs java"[size=1em]>[size=1em]package [size=1em]java_interview; [size=1em]import [size=1em]java.io.File; [size=1em]import [size=1em]java.io.FileInputStream; [size=1em]import [size=1em]java.io.FileOutputStream; [size=1em]import [size=1em]java.io.ObjectInputStream; [size=1em]import [size=1em]java.io.ObjectOutputStream; [size=1em]import [size=1em]java.io.Serializable; [size=1em]import [size=1em]java.lang.reflect.Field; [size=1em]import [size=1em]java.util.ArrayList; [size=1em]import [size=1em]java.util.Arrays; [size=1em]import [size=1em]java.util.Collections; [size=1em]import [size=1em]java.util.Comparator; [size=1em]import [size=1em]java.util.HashMap; [size=1em]import [size=1em]java.util.HashSet; [size=1em]import [size=1em]java.util.Iterator; [size=1em]import [size=1em]java.util.List; [size=1em]import [size=1em]java.util.Map; [size=1em]import [size=1em]java.util.NavigableMap; [size=1em]import [size=1em]java.util.Random; [size=1em]import [size=1em]java.util.Scanner; [size=1em]import [size=1em]java.util.TreeMap; [size=1em]import [size=1em]java.util.TreeSet; [size=1em]public [size=1em]class [size=1em]Test { [size=1em] [size=1em]public [size=1em]static [size=1em]void [size=1em]main(String[] args) [size=1em]throws [size=1em]Exception { [size=1em] [size=1em]} [size=1em]} [size=1em] [size=1em]enum [size=1em]Gender { [size=1em] [size=1em]MALE, FEMALE [size=1em]} [size=1em]class [size=1em]Person [size=1em]implements [size=1em]Serializable { [size=1em] [size=1em]private [size=1em]String name = [size=1em]null[size=1em]; [size=1em] [size=1em]private [size=1em]Integer age = [size=1em]null[size=1em]; [size=1em] [size=1em]private [size=1em]Gender gender = [size=1em]null[size=1em]; [size=1em] [size=1em]public [size=1em]Person() { [size=1em] [size=1em]System.out.println([size=1em]"none-arg constructor"[size=1em]); [size=1em] [size=1em]} [size=1em] [size=1em]public [size=1em]Person(String name, Integer age, Gender gender) { [size=1em] [size=1em]System.out.println([size=1em]"arg constructor"[size=1em]); [size=1em] [size=1em]this[size=1em].name = name; [size=1em] [size=1em]this[size=1em].age = age; [size=1em] [size=1em]this[size=1em].gender = gender; [size=1em] [size=1em]} [size=1em] [size=1em]public [size=1em]String getName() { [size=1em] [size=1em]return [size=1em]name; [size=1em] [size=1em]} [size=1em] [size=1em]public [size=1em]void [size=1em]setName(String name) { [size=1em] [size=1em]this[size=1em].name = name; [size=1em] [size=1em]} [size=1em] [size=1em]public [size=1em]Integer getAge() { [size=1em] [size=1em]return [size=1em]age; [size=1em] [size=1em]} [size=1em] [size=1em]public [size=1em]void [size=1em]setAge(Integer age) { [size=1em] [size=1em]this[size=1em].age = age; [size=1em] [size=1em]} [size=1em] [size=1em]public [size=1em]Gender getGender() { [size=1em] [size=1em]return [size=1em]gender; [size=1em] [size=1em]} [size=1em] [size=1em]public [size=1em]void [size=1em]setGender(Gender gender) { [size=1em] [size=1em]this[size=1em].gender = gender; [size=1em] [size=1em]} [size=1em] [size=1em]@Override [size=1em] [size=1em]public [size=1em]String toString() { [size=1em] [size=1em]return [size=1em]"[" [size=1em]+ name + [size=1em]", " [size=1em]+ age + [size=1em]", " [size=1em]+ gender + [size=1em]"]"[size=1em]; [size=1em] [size=1em]} [size=1em]} [size=1em]public [size=1em]class [size=1em]SimpleSerial { [size=1em] [size=1em]public [size=1em]static [size=1em]void [size=1em]main(String[] args) [size=1em]throws [size=1em]Exception { [size=1em] [size=1em]File file = [size=1em]new [size=1em]File([size=1em]"person.out"[size=1em]); [size=1em] [size=1em]ObjectOutputStream oout = [size=1em]new [size=1em]ObjectOutputStream([size=1em]new [size=1em]FileOutputStream(file)); [size=1em] [size=1em]Person person = [size=1em]new [size=1em]Person([size=1em]"John"[size=1em], [size=1em]101[size=1em], Gender.MALE); [size=1em] [size=1em]oout.writeObject(person); [size=1em] [size=1em]oout.close(); [size=1em] [size=1em]ObjectInputStream oin = [size=1em]new [size=1em]ObjectInputStream([size=1em]new [size=1em]FileInputStream(file)); [size=1em] [size=1em]Object newPerson = oin.readObject(); [size=1em]// 没有强制转换到Person类型 [size=1em] [size=1em]oin.close(); [size=1em] [size=1em]System.out.println(newPerson); [size=1em] [size=1em]} [size=1em]} [size=1em]/*输出: [size=1em]arg constructor [size=1em][John, 101, MALE] [size=1em]*/ [size=1em]</code> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | [size=1em]<code [size=1em]class[size=1em]=[size=1em]"hljs java"[size=1em]>[size=1em]private [size=1em]void [size=1em]writeObject0(Object obj, [size=1em]boolean [size=1em]unshared) [size=1em]throws [size=1em]IOException { [size=1em] [size=1em]if [size=1em](obj [size=1em]instanceof [size=1em]String) { [size=1em] [size=1em]writeString((String) obj, unshared); [size=1em] [size=1em]} [size=1em]else [size=1em]if [size=1em](cl.isArray()) { [size=1em] [size=1em]writeArray(obj, desc, unshared); [size=1em] [size=1em]} [size=1em]else [size=1em]if [size=1em](obj [size=1em]instanceof [size=1em]Enum) { [size=1em] [size=1em]writeEnum((Enum) obj, desc, unshared); [size=1em] [size=1em]} [size=1em]else [size=1em]if [size=1em](obj [size=1em]instanceof [size=1em]Serializable) { [size=1em] [size=1em]writeOrdinaryObject(obj, desc, unshared); [size=1em] [size=1em]} [size=1em]else [size=1em]{ [size=1em] [size=1em]if [size=1em](extendedDebugInfo) { [size=1em] [size=1em]throw [size=1em]new [size=1em]NotSerializableException(cl.getName() + [size=1em]"\n" [size=1em] [size=1em]+ debugInfoStack.toString()); [size=1em] [size=1em]} [size=1em]else [size=1em]{ [size=1em] [size=1em]throw [size=1em]new [size=1em]NotSerializableException(cl.getName()); [size=1em] [size=1em]} [size=1em] [size=1em]} [size=1em]}</code> |
1 2 3 4 5 | [size=1em]<code [size=1em]class[size=1em]=[size=1em]"hljs java"[size=1em]>[size=1em]public [size=1em]class [size=1em]Person [size=1em]implements [size=1em]Serializable { [size=1em] [size=1em]transient [size=1em]private [size=1em]Integer age = [size=1em]null[size=1em]; [size=1em]}</code> |
1 2 | [size=1em]<code [size=1em]class[size=1em]=[size=1em]"hljs delphi"[size=1em]>arg constructor [size=1em][John, [size=1em]null[size=1em], MALE]</code> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | [size=1em]<code [size=1em]class[size=1em]=[size=1em]"hljs java"[size=1em]>[size=1em]public [size=1em]class [size=1em]Person [size=1em]implements [size=1em]Serializable { [size=1em] [size=1em]transient [size=1em]private [size=1em]Integer age = [size=1em]null[size=1em]; [size=1em] [size=1em]private [size=1em]void [size=1em]writeObject(ObjectOutputStream out) [size=1em]throws [size=1em]IOException { [size=1em] [size=1em]out.defaultWriteObject(); [size=1em] [size=1em]out.writeInt(age); [size=1em] [size=1em]} [size=1em] [size=1em]private [size=1em]void [size=1em]readObject(ObjectInputStream in) [size=1em]throws [size=1em]IOException, ClassNotFoundException { [size=1em] [size=1em]in.defaultReadObject(); [size=1em] [size=1em]age = in.readInt(); [size=1em] [size=1em]} [size=1em]}</code> |
1 2 | [size=1em]<code [size=1em]class[size=1em]=[size=1em]"hljs delphi"[size=1em]>arg constructor [size=1em][John, [size=1em]31[size=1em], MALE]</code> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | [size=1em]<code [size=1em]class[size=1em]=[size=1em]"hljs java"[size=1em]>[size=1em]public [size=1em]class [size=1em]Person [size=1em]implements [size=1em]Externalizable { [size=1em] [size=1em]private [size=1em]String name = [size=1em]null[size=1em]; [size=1em] [size=1em]transient [size=1em]private [size=1em]Integer age = [size=1em]null[size=1em]; [size=1em] [size=1em]private [size=1em]Gender gender = [size=1em]null[size=1em]; [size=1em] [size=1em]public [size=1em]Person() { [size=1em] [size=1em]System.out.println([size=1em]"none-arg constructor"[size=1em]); [size=1em] [size=1em]} [size=1em] [size=1em]public [size=1em]Person(String name, Integer age, Gender gender) { [size=1em] [size=1em]System.out.println([size=1em]"arg constructor"[size=1em]); [size=1em] [size=1em]this[size=1em].name = name; [size=1em] [size=1em]this[size=1em].age = age; [size=1em] [size=1em]this[size=1em].gender = gender; [size=1em] [size=1em]} [size=1em] [size=1em]private [size=1em]void [size=1em]writeObject(ObjectOutputStream out) [size=1em]throws [size=1em]IOException { [size=1em] [size=1em]out.defaultWriteObject(); [size=1em] [size=1em]out.writeInt(age); [size=1em] [size=1em]} [size=1em] [size=1em]private [size=1em]void [size=1em]readObject(ObjectInputStream in) [size=1em]throws [size=1em]IOException, ClassNotFoundException { [size=1em] [size=1em]in.defaultReadObject(); [size=1em] [size=1em]age = in.readInt(); [size=1em] [size=1em]} [size=1em] [size=1em]@Override [size=1em] [size=1em]public [size=1em]void [size=1em]writeExternal(ObjectOutput out) [size=1em]throws [size=1em]IOException { [size=1em] [size=1em]} [size=1em] [size=1em]@Override [size=1em] [size=1em]public [size=1em]void [size=1em]readExternal(ObjectInput in) [size=1em]throws [size=1em]IOException, ClassNotFoundException { [size=1em] [size=1em]} [size=1em]}</code> |
1 2 3 | [size=1em]<code [size=1em]class[size=1em]=[size=1em]"hljs delphi"[size=1em]>arg constructor [size=1em]none-arg constructor [size=1em][[size=1em]null[size=1em], [size=1em]null[size=1em], [size=1em]null[size=1em]]</code> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | [size=1em]<code [size=1em]class[size=1em]=[size=1em]"hljs java"[size=1em]>[size=1em]public [size=1em]class [size=1em]Person [size=1em]implements [size=1em]Externalizable { [size=1em] [size=1em]private [size=1em]String name = [size=1em]null[size=1em]; [size=1em] [size=1em]transient [size=1em]private [size=1em]Integer age = [size=1em]null[size=1em]; [size=1em] [size=1em]private [size=1em]Gender gender = [size=1em]null[size=1em]; [size=1em] [size=1em]public [size=1em]Person() { [size=1em] [size=1em]System.out.println([size=1em]"none-arg constructor"[size=1em]); [size=1em] [size=1em]} [size=1em] [size=1em]public [size=1em]Person(String name, Integer age, Gender gender) { [size=1em] [size=1em]System.out.println([size=1em]"arg constructor"[size=1em]); [size=1em] [size=1em]this[size=1em].name = name; [size=1em] [size=1em]this[size=1em].age = age; [size=1em] [size=1em]this[size=1em].gender = gender; [size=1em] [size=1em]} [size=1em] [size=1em]private [size=1em]void [size=1em]writeObject(ObjectOutputStream out) [size=1em]throws [size=1em]IOException { [size=1em] [size=1em]out.defaultWriteObject(); [size=1em] [size=1em]out.writeInt(age); [size=1em] [size=1em]} [size=1em] [size=1em]private [size=1em]void [size=1em]readObject(ObjectInputStream in) [size=1em]throws [size=1em]IOException, ClassNotFoundException { [size=1em] [size=1em]in.defaultReadObject(); [size=1em] [size=1em]age = in.readInt(); [size=1em] [size=1em]} [size=1em] [size=1em]@Override [size=1em] [size=1em]public [size=1em]void [size=1em]writeExternal(ObjectOutput out) [size=1em]throws [size=1em]IOException { [size=1em] [size=1em]out.writeObject(name); [size=1em] [size=1em]out.writeInt(age); [size=1em] [size=1em]} [size=1em] [size=1em]@Override [size=1em] [size=1em]public [size=1em]void [size=1em]readExternal(ObjectInput in) [size=1em]throws [size=1em]IOException, ClassNotFoundException { [size=1em] [size=1em]name = (String) in.readObject(); [size=1em] [size=1em]age = in.readInt(); [size=1em] [size=1em]} [size=1em]}</code> |
1 2 3 | [size=1em]<code [size=1em]class[size=1em]=[size=1em]"hljs delphi"[size=1em]>arg constructor [size=1em]none-arg constructor [size=1em][John, [size=1em]31[size=1em], [size=1em]null[size=1em]]</code> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | [size=1em]<code [size=1em]class[size=1em]=[size=1em]"hljs java"[size=1em]>[size=1em]public [size=1em]class [size=1em]Person [size=1em]implements [size=1em]Serializable { [size=1em] [size=1em]private [size=1em]static [size=1em]class [size=1em]InstanceHolder { [size=1em] [size=1em]private [size=1em]static [size=1em]final [size=1em]Person instatnce = [size=1em]new [size=1em]Person([size=1em]"John"[size=1em], [size=1em]31[size=1em], Gender.MALE); [size=1em] [size=1em]} [size=1em] [size=1em]public [size=1em]static [size=1em]Person getInstance() { [size=1em] [size=1em]return [size=1em]InstanceHolder.instatnce; [size=1em] [size=1em]} [size=1em] [size=1em]private [size=1em]String name = [size=1em]null[size=1em]; [size=1em] [size=1em]private [size=1em]Integer age = [size=1em]null[size=1em]; [size=1em] [size=1em]private [size=1em]Gender gender = [size=1em]null[size=1em]; [size=1em] [size=1em]private [size=1em]Person() { [size=1em] [size=1em]System.out.println([size=1em]"none-arg constructor"[size=1em]); [size=1em] [size=1em]} [size=1em] [size=1em]private [size=1em]Person(String name, Integer age, Gender gender) { [size=1em] [size=1em]System.out.println([size=1em]"arg constructor"[size=1em]); [size=1em] [size=1em]this[size=1em].name = name; [size=1em] [size=1em]this[size=1em].age = age; [size=1em] [size=1em]this[size=1em].gender = gender; [size=1em] [size=1em]} [size=1em]}</code> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | [size=1em]<code [size=1em]class[size=1em]=[size=1em]"hljs cs"[size=1em]>[size=1em]public [size=1em]class [size=1em]SimpleSerial { [size=1em] [size=1em]public [size=1em]static [size=1em]void [size=1em]main(String[] args) [size=1em]throws [size=1em]Exception { [size=1em] [size=1em]File file = [size=1em]new [size=1em]File([size=1em]"person.out"[size=1em]); [size=1em] [size=1em]ObjectOutputStream oout = [size=1em]new [size=1em]ObjectOutputStream([size=1em]new [size=1em]FileOutputStream(file)); [size=1em] [size=1em]oout.writeObject(Person.getInstance()); [size=1em]// 保存单例对象 [size=1em] [size=1em]oout.close(); [size=1em] [size=1em]ObjectInputStream oin = [size=1em]new [size=1em]ObjectInputStream([size=1em]new [size=1em]FileInputStream(file)); [size=1em] [size=1em]Object newPerson = oin.readObject(); [size=1em] [size=1em]oin.close(); [size=1em] [size=1em]System.out.println(newPerson); [size=1em] [size=1em]System.out.println(Person.getInstance() == newPerson); [size=1em]// 将获取的对象与Person类中的单例对象进行相等性比较 [size=1em] [size=1em]} [size=1em]}</code> |
1 2 3 | [size=1em]<code [size=1em]class[size=1em]=[size=1em]"hljs delphi"[size=1em]>arg constructor [size=1em][John, [size=1em]31[size=1em], MALE] [size=1em]false[size=1em]</code> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | [size=1em]<code [size=1em]class[size=1em]=[size=1em]"hljs java"[size=1em]>[size=1em]public [size=1em]class [size=1em]Person [size=1em]implements [size=1em]Serializable { [size=1em] [size=1em]private [size=1em]static [size=1em]class [size=1em]InstanceHolder { [size=1em] [size=1em]private [size=1em]static [size=1em]final [size=1em]Person instatnce = [size=1em]new [size=1em]Person([size=1em]"John"[size=1em], [size=1em]31[size=1em], Gender.MALE); [size=1em] [size=1em]} [size=1em] [size=1em]public [size=1em]static [size=1em]Person getInstance() { [size=1em] [size=1em]return [size=1em]InstanceHolder.instatnce; [size=1em] [size=1em]} [size=1em] [size=1em]private [size=1em]String name = [size=1em]null[size=1em]; [size=1em] [size=1em]private [size=1em]Integer age = [size=1em]null[size=1em]; [size=1em] [size=1em]private [size=1em]Gender gender = [size=1em]null[size=1em]; [size=1em] [size=1em]private [size=1em]Person() { [size=1em] [size=1em]System.out.println([size=1em]"none-arg constructor"[size=1em]); [size=1em] [size=1em]} [size=1em] [size=1em]private [size=1em]Person(String name, Integer age, Gender gender) { [size=1em] [size=1em]System.out.println([size=1em]"arg constructor"[size=1em]); [size=1em] [size=1em]this[size=1em].name = name; [size=1em] [size=1em]this[size=1em].age = age; [size=1em] [size=1em]this[size=1em].gender = gender; [size=1em] [size=1em]} [size=1em] [size=1em]private [size=1em]Object readResolve() [size=1em]throws [size=1em]ObjectStreamException { [size=1em] [size=1em]return [size=1em]InstanceHolder.instatnce; [size=1em] [size=1em]} [size=1em]}</code> |
1 2 3 | [size=1em]<code [size=1em]class[size=1em]=[size=1em]"hljs delphi"[size=1em]>arg constructor [size=1em][John, [size=1em]31[size=1em], MALE] [size=1em]true[size=1em]</code> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | [size=1em]<code [size=1em]class[size=1em]=[size=1em]"hljs java"[size=1em]> [size=1em]package [size=1em]com.inout; [size=1em] [size=1em]import [size=1em]java.io.Serializable; [size=1em] [size=1em]public [size=1em]class [size=1em]A [size=1em]implements [size=1em]Serializable { [size=1em] [size=1em]private [size=1em]static [size=1em]final [size=1em]long [size=1em]serialVersionUID = 1L; [size=1em] [size=1em]private [size=1em]String name; [size=1em] [size=1em]public [size=1em]String getName() [size=1em] [size=1em]{ [size=1em] [size=1em]return [size=1em]name; [size=1em] [size=1em]} [size=1em] [size=1em]public [size=1em]void [size=1em]setName(String name) [size=1em] [size=1em]{ [size=1em] [size=1em]this[size=1em].name = name; [size=1em] [size=1em]} [size=1em] [size=1em]} [size=1em] [size=1em]package [size=1em]com.inout; [size=1em] [size=1em]import [size=1em]java.io.Serializable; [size=1em] [size=1em]public [size=1em]class [size=1em]A [size=1em]implements [size=1em]Serializable { [size=1em] [size=1em]private [size=1em]static [size=1em]final [size=1em]long [size=1em]serialVersionUID = 2L; [size=1em] [size=1em]private [size=1em]String name; [size=1em] [size=1em]public [size=1em]String getName() [size=1em] [size=1em]{ [size=1em] [size=1em]return [size=1em]name; [size=1em] [size=1em]} [size=1em] [size=1em]public [size=1em]void [size=1em]setName(String name) [size=1em] [size=1em]{ [size=1em] [size=1em]this[size=1em].name = name; [size=1em]</code> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | [size=1em]<code [size=1em]class[size=1em]=[size=1em]"hljs avrasm"[size=1em]>ObjectOutputStream out = [size=1em]new [size=1em]ObjectOutputStream( [size=1em] [size=1em]new [size=1em]FileOutputStream([size=1em]"result.obj"[size=1em])); [size=1em] [size=1em]Test test = [size=1em]new [size=1em]Test(); [size=1em] [size=1em]//试图将对象两次写入文件 [size=1em] [size=1em]out.writeObject(test); [size=1em] [size=1em]out.flush(); [size=1em] [size=1em]System.out.println([size=1em]new [size=1em]File([size=1em]"result.obj"[size=1em]).length()); [size=1em] [size=1em]out.writeObject(test); [size=1em] [size=1em]out.close(); [size=1em] [size=1em]System.out.println([size=1em]new [size=1em]File([size=1em]"result.obj"[size=1em]).length()); [size=1em] [size=1em]ObjectInputStream oin = [size=1em]new [size=1em]ObjectInputStream([size=1em]new [size=1em]FileInputStream( [size=1em] [size=1em]"result.obj"[size=1em])); [size=1em] [size=1em]//从文件依次读出两个文件 [size=1em] [size=1em]Test t1 = (Test) oin.readObject(); [size=1em] [size=1em]Test t2 = (Test) oin.readObject(); [size=1em] [size=1em]oin.close(); [size=1em] [size=1em]//判断两个引用是否指向同一个对象 [size=1em] [size=1em]System.out.println(t1 == t2); [size=1em]/* [size=1em]31 [size=1em]36 [size=1em]true [size=1em]*/ [size=1em]</code> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 | [size=1em]<code [size=1em]class[size=1em]=[size=1em]"hljs java"[size=1em]>[size=1em]package [size=1em]java_interview; [size=1em]import [size=1em]java.io.File; [size=1em]import [size=1em]java.io.FileInputStream; [size=1em]import [size=1em]java.io.FileOutputStream; [size=1em]import [size=1em]java.io.ObjectInputStream; [size=1em]import [size=1em]java.io.ObjectOutputStream; [size=1em]import [size=1em]java.io.Serializable; [size=1em]import [size=1em]java.lang.reflect.Field; [size=1em]import [size=1em]java.util.ArrayList; [size=1em]import [size=1em]java.util.Arrays; [size=1em]import [size=1em]java.util.Collections; [size=1em]import [size=1em]java.util.Comparator; [size=1em]import [size=1em]java.util.HashMap; [size=1em]import [size=1em]java.util.HashSet; [size=1em]import [size=1em]java.util.Iterator; [size=1em]import [size=1em]java.util.List; [size=1em]import [size=1em]java.util.Map; [size=1em]import [size=1em]java.util.NavigableMap; [size=1em]import [size=1em]java.util.Random; [size=1em]import [size=1em]java.util.Scanner; [size=1em]import [size=1em]java.util.TreeMap; [size=1em]import [size=1em]java.util.TreeSet; [size=1em]public [size=1em]class [size=1em]Test { [size=1em] [size=1em]public [size=1em]static [size=1em]void [size=1em]main(String[] args) [size=1em]throws [size=1em]Exception { [size=1em] [size=1em]} [size=1em]} [size=1em] [size=1em]enum [size=1em]Gender { [size=1em] [size=1em]MALE, FEMALE [size=1em]} [size=1em]class [size=1em]Person [size=1em]implements [size=1em]Serializable { [size=1em] [size=1em]private [size=1em]String name = [size=1em]null[size=1em]; [size=1em] [size=1em]private [size=1em]Integer age = [size=1em]null[size=1em]; [size=1em] [size=1em]private [size=1em]Gender gender = [size=1em]null[size=1em]; [size=1em] [size=1em]public [size=1em]Person() { [size=1em] [size=1em]System.out.println([size=1em]"none-arg constructor"[size=1em]); [size=1em] [size=1em]} [size=1em] [size=1em]public [size=1em]Person(String name, Integer age, Gender gender) { [size=1em] [size=1em]System.out.println([size=1em]"arg constructor"[size=1em]); [size=1em] [size=1em]this[size=1em].name = name; [size=1em] [size=1em]this[size=1em].age = age; [size=1em] [size=1em]this[size=1em].gender = gender; [size=1em] [size=1em]} [size=1em] [size=1em]public [size=1em]String getName() { [size=1em] [size=1em]return [size=1em]name; [size=1em] [size=1em]} [size=1em] [size=1em]public [size=1em]void [size=1em]setName(String name) { [size=1em] [size=1em]this[size=1em].name = name; [size=1em] [size=1em]} [size=1em] [size=1em]public [size=1em]Integer getAge() { [size=1em] [size=1em]return [size=1em]age; [size=1em] [size=1em]} [size=1em] [size=1em]public [size=1em]void [size=1em]setAge(Integer age) { [size=1em] [size=1em]this[size=1em].age = age; [size=1em] [size=1em]} [size=1em] [size=1em]public [size=1em]Gender getGender() { [size=1em] [size=1em]return [size=1em]gender; [size=1em] [size=1em]} [size=1em] [size=1em]public [size=1em]void [size=1em]setGender(Gender gender) { [size=1em] [size=1em]this[size=1em].gender = gender; [size=1em] [size=1em]} [size=1em] [size=1em]@Override [size=1em] [size=1em]public [size=1em]String toString() { [size=1em] [size=1em]return [size=1em]"[" [size=1em]+ name + [size=1em]", " [size=1em]+ age + [size=1em]", " [size=1em]+ gender + [size=1em]"]"[size=1em]; [size=1em] [size=1em]} [size=1em]} [size=1em]public [size=1em]class [size=1em]SimpleSerial { [size=1em] [size=1em]public [size=1em]static [size=1em]void [size=1em]main(String[] args) [size=1em]throws [size=1em]Exception { [size=1em] [size=1em]File file = [size=1em]new [size=1em]File([size=1em]"person.out"[size=1em]); [size=1em] [size=1em]ObjectOutputStream oout = [size=1em]new [size=1em]ObjectOutputStream([size=1em]new [size=1em]FileOutputStream(file)); [size=1em] [size=1em]Person person = [size=1em]new [size=1em]Person([size=1em]"John"[size=1em], [size=1em]101[size=1em], Gender.MALE); [size=1em] [size=1em]oout.writeObject(person); [size=1em] [size=1em]oout.close(); [size=1em] [size=1em]ObjectInputStream oin = [size=1em]new [size=1em]ObjectInputStream([size=1em]new [size=1em]FileInputStream(file)); [size=1em] [size=1em]Object newPerson = oin.readObject(); [size=1em]// 没有强制转换到Person类型 [size=1em] [size=1em]oin.close(); [size=1em] [size=1em]System.out.println(newPerson); [size=1em] [size=1em]} [size=1em]} [size=1em]/*输出: [size=1em]arg constructor [size=1em][John, 101, MALE] [size=1em]*/ [size=1em]</code> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | [size=1em]<code [size=1em]class[size=1em]=[size=1em]"hljs java"[size=1em]>[size=1em]private [size=1em]void [size=1em]writeObject0(Object obj, [size=1em]boolean [size=1em]unshared) [size=1em]throws [size=1em]IOException { [size=1em] [size=1em]if [size=1em](obj [size=1em]instanceof [size=1em]String) { [size=1em] [size=1em]writeString((String) obj, unshared); [size=1em] [size=1em]} [size=1em]else [size=1em]if [size=1em](cl.isArray()) { [size=1em] [size=1em]writeArray(obj, desc, unshared); [size=1em] [size=1em]} [size=1em]else [size=1em]if [size=1em](obj [size=1em]instanceof [size=1em]Enum) { [size=1em] [size=1em]writeEnum((Enum) obj, desc, unshared); [size=1em] [size=1em]} [size=1em]else [size=1em]if [size=1em](obj [size=1em]instanceof [size=1em]Serializable) { [size=1em] [size=1em]writeOrdinaryObject(obj, desc, unshared); [size=1em] [size=1em]} [size=1em]else [size=1em]{ [size=1em] [size=1em]if [size=1em](extendedDebugInfo) { [size=1em] [size=1em]throw [size=1em]new [size=1em]NotSerializableException(cl.getName() + [size=1em]"\n" [size=1em] [size=1em]+ debugInfoStack.toString()); [size=1em] [size=1em]} [size=1em]else [size=1em]{ [size=1em] [size=1em]throw [size=1em]new [size=1em]NotSerializableException(cl.getName()); [size=1em] [size=1em]} [size=1em] [size=1em]} [size=1em]}</code> |
1 2 3 4 5 | [size=1em]<code [size=1em]class[size=1em]=[size=1em]"hljs java"[size=1em]>[size=1em]public [size=1em]class [size=1em]Person [size=1em]implements [size=1em]Serializable { [size=1em] [size=1em]transient [size=1em]private [size=1em]Integer age = [size=1em]null[size=1em]; [size=1em]}</code> |
1 2 | [size=1em]<code [size=1em]class[size=1em]=[size=1em]"hljs delphi"[size=1em]>arg constructor [size=1em][John, [size=1em]null[size=1em], MALE]</code> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | [size=1em]<code [size=1em]class[size=1em]=[size=1em]"hljs java"[size=1em]>[size=1em]public [size=1em]class [size=1em]Person [size=1em]implements [size=1em]Serializable { [size=1em] [size=1em]transient [size=1em]private [size=1em]Integer age = [size=1em]null[size=1em]; [size=1em] [size=1em]private [size=1em]void [size=1em]writeObject(ObjectOutputStream out) [size=1em]throws [size=1em]IOException { [size=1em] [size=1em]out.defaultWriteObject(); [size=1em] [size=1em]out.writeInt(age); [size=1em] [size=1em]} [size=1em] [size=1em]private [size=1em]void [size=1em]readObject(ObjectInputStream in) [size=1em]throws [size=1em]IOException, ClassNotFoundException { [size=1em] [size=1em]in.defaultReadObject(); [size=1em] [size=1em]age = in.readInt(); [size=1em] [size=1em]} [size=1em]}</code> |
1 2 | [size=1em]<code [size=1em]class[size=1em]=[size=1em]"hljs delphi"[size=1em]>arg constructor [size=1em][John, [size=1em]31[size=1em], MALE]</code> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | [size=1em]<code [size=1em]class[size=1em]=[size=1em]"hljs java"[size=1em]>[size=1em]public [size=1em]class [size=1em]Person [size=1em]implements [size=1em]Externalizable { [size=1em] [size=1em]private [size=1em]String name = [size=1em]null[size=1em]; [size=1em] [size=1em]transient [size=1em]private [size=1em]Integer age = [size=1em]null[size=1em]; [size=1em] [size=1em]private [size=1em]Gender gender = [size=1em]null[size=1em]; [size=1em] [size=1em]public [size=1em]Person() { [size=1em] [size=1em]System.out.println([size=1em]"none-arg constructor"[size=1em]); [size=1em] [size=1em]} [size=1em] [size=1em]public [size=1em]Person(String name, Integer age, Gender gender) { [size=1em] [size=1em]System.out.println([size=1em]"arg constructor"[size=1em]); [size=1em] [size=1em]this[size=1em].name = name; [size=1em] [size=1em]this[size=1em].age = age; [size=1em] [size=1em]this[size=1em].gender = gender; [size=1em] [size=1em]} [size=1em] [size=1em]private [size=1em]void [size=1em]writeObject(ObjectOutputStream out) [size=1em]throws [size=1em]IOException { [size=1em] [size=1em]out.defaultWriteObject(); [size=1em] [size=1em]out.writeInt(age); [size=1em] [size=1em]} [size=1em] [size=1em]private [size=1em]void [size=1em]readObject(ObjectInputStream in) [size=1em]throws [size=1em]IOException, ClassNotFoundException { [size=1em] [size=1em]in.defaultReadObject(); [size=1em] [size=1em]age = in.readInt(); [size=1em] [size=1em]} [size=1em] [size=1em]@Override [size=1em] [size=1em]public [size=1em]void [size=1em]writeExternal(ObjectOutput out) [size=1em]throws [size=1em]IOException { [size=1em] [size=1em]} [size=1em] [size=1em]@Override [size=1em] [size=1em]public [size=1em]void [size=1em]readExternal(ObjectInput in) [size=1em]throws [size=1em]IOException, ClassNotFoundException { [size=1em] [size=1em]} [size=1em]}</code> |
1 2 3 | [size=1em]<code [size=1em]class[size=1em]=[size=1em]"hljs delphi"[size=1em]>arg constructor [size=1em]none-arg constructor [size=1em][[size=1em]null[size=1em], [size=1em]null[size=1em], [size=1em]null[size=1em]]</code> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | [size=1em]<code [size=1em]class[size=1em]=[size=1em]"hljs java"[size=1em]>[size=1em]public [size=1em]class [size=1em]Person [size=1em]implements [size=1em]Externalizable { [size=1em] [size=1em]private [size=1em]String name = [size=1em]null[size=1em]; [size=1em] [size=1em]transient [size=1em]private [size=1em]Integer age = [size=1em]null[size=1em]; [size=1em] [size=1em]private [size=1em]Gender gender = [size=1em]null[size=1em]; [size=1em] [size=1em]public [size=1em]Person() { [size=1em] [size=1em]System.out.println([size=1em]"none-arg constructor"[size=1em]); [size=1em] [size=1em]} [size=1em] [size=1em]public [size=1em]Person(String name, Integer age, Gender gender) { [size=1em] [size=1em]System.out.println([size=1em]"arg constructor"[size=1em]); [size=1em] [size=1em]this[size=1em].name = name; [size=1em] [size=1em]this[size=1em].age = age; [size=1em] [size=1em]this[size=1em].gender = gender; [size=1em] [size=1em]} [size=1em] [size=1em]private [size=1em]void [size=1em]writeObject(ObjectOutputStream out) [size=1em]throws [size=1em]IOException { [size=1em] [size=1em]out.defaultWriteObject(); [size=1em] [size=1em]out.writeInt(age); [size=1em] [size=1em]} [size=1em] [size=1em]private [size=1em]void [size=1em]readObject(ObjectInputStream in) [size=1em]throws [size=1em]IOException, ClassNotFoundException { [size=1em] [size=1em]in.defaultReadObject(); [size=1em] [size=1em]age = in.readInt(); [size=1em] [size=1em]} [size=1em] [size=1em]@Override [size=1em] [size=1em]public [size=1em]void [size=1em]writeExternal(ObjectOutput out) [size=1em]throws [size=1em]IOException { [size=1em] [size=1em]out.writeObject(name); [size=1em] [size=1em]out.writeInt(age); [size=1em] [size=1em]} [size=1em] [size=1em]@Override [size=1em] [size=1em]public [size=1em]void [size=1em]readExternal(ObjectInput in) [size=1em]throws [size=1em]IOException, ClassNotFoundException { [size=1em] [size=1em]name = (String) in.readObject(); [size=1em] [size=1em]age = in.readInt(); [size=1em] [size=1em]} [size=1em]}</code> |
1 2 3 | [size=1em]<code [size=1em]class[size=1em]=[size=1em]"hljs delphi"[size=1em]>arg constructor [size=1em]none-arg constructor [size=1em][John, [size=1em]31[size=1em], [size=1em]null[size=1em]]</code> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | [size=1em]<code [size=1em]class[size=1em]=[size=1em]"hljs java"[size=1em]>[size=1em]public [size=1em]class [size=1em]Person [size=1em]implements [size=1em]Serializable { [size=1em] [size=1em]private [size=1em]static [size=1em]class [size=1em]InstanceHolder { [size=1em] [size=1em]private [size=1em]static [size=1em]final [size=1em]Person instatnce = [size=1em]new [size=1em]Person([size=1em]"John"[size=1em], [size=1em]31[size=1em], Gender.MALE); [size=1em] [size=1em]} [size=1em] [size=1em]public [size=1em]static [size=1em]Person getInstance() { [size=1em] [size=1em]return [size=1em]InstanceHolder.instatnce; [size=1em] [size=1em]} [size=1em] [size=1em]private [size=1em]String name = [size=1em]null[size=1em]; [size=1em] [size=1em]private [size=1em]Integer age = [size=1em]null[size=1em]; [size=1em] [size=1em]private [size=1em]Gender gender = [size=1em]null[size=1em]; [size=1em] [size=1em]private [size=1em]Person() { [size=1em] [size=1em]System.out.println([size=1em]"none-arg constructor"[size=1em]); [size=1em] [size=1em]} [size=1em] [size=1em]private [size=1em]Person(String name, Integer age, Gender gender) { [size=1em] [size=1em]System.out.println([size=1em]"arg constructor"[size=1em]); [size=1em] [size=1em]this[size=1em].name = name; [size=1em] [size=1em]this[size=1em].age = age; [size=1em] [size=1em]this[size=1em].gender = gender; [size=1em] [size=1em]} [size=1em]}</code> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | [size=1em]<code [size=1em]class[size=1em]=[size=1em]"hljs cs"[size=1em]>[size=1em]public [size=1em]class [size=1em]SimpleSerial { [size=1em] [size=1em]public [size=1em]static [size=1em]void [size=1em]main(String[] args) [size=1em]throws [size=1em]Exception { [size=1em] [size=1em]File file = [size=1em]new [size=1em]File([size=1em]"person.out"[size=1em]); [size=1em] [size=1em]ObjectOutputStream oout = [size=1em]new [size=1em]ObjectOutputStream([size=1em]new [size=1em]FileOutputStream(file)); [size=1em] [size=1em]oout.writeObject(Person.getInstance()); [size=1em]// 保存单例对象 [size=1em] [size=1em]oout.close(); [size=1em] [size=1em]ObjectInputStream oin = [size=1em]new [size=1em]ObjectInputStream([size=1em]new [size=1em]FileInputStream(file)); [size=1em] [size=1em]Object newPerson = oin.readObject(); [size=1em] [size=1em]oin.close(); [size=1em] [size=1em]System.out.println(newPerson); [size=1em] [size=1em]System.out.println(Person.getInstance() == newPerson); [size=1em]// 将获取的对象与Person类中的单例对象进行相等性比较 [size=1em] [size=1em]} [size=1em]}</code> |
1 2 3 | [size=1em]<code [size=1em]class[size=1em]=[size=1em]"hljs delphi"[size=1em]>arg constructor [size=1em][John, [size=1em]31[size=1em], MALE] [size=1em]false[size=1em]</code> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | [size=1em]<code [size=1em]class[size=1em]=[size=1em]"hljs java"[size=1em]>[size=1em]public [size=1em]class [size=1em]Person [size=1em]implements [size=1em]Serializable { [size=1em] [size=1em]private [size=1em]static [size=1em]class [size=1em]InstanceHolder { [size=1em] [size=1em]private [size=1em]static [size=1em]final [size=1em]Person instatnce = [size=1em]new [size=1em]Person([size=1em]"John"[size=1em], [size=1em]31[size=1em], Gender.MALE); [size=1em] [size=1em]} [size=1em] [size=1em]public [size=1em]static [size=1em]Person getInstance() { [size=1em] [size=1em]return [size=1em]InstanceHolder.instatnce; [size=1em] [size=1em]} [size=1em] [size=1em]private [size=1em]String name = [size=1em]null[size=1em]; [size=1em] [size=1em]private [size=1em]Integer age = [size=1em]null[size=1em]; [size=1em] [size=1em]private [size=1em]Gender gender = [size=1em]null[size=1em]; [size=1em] [size=1em]private [size=1em]Person() { [size=1em] [size=1em]System.out.println([size=1em]"none-arg constructor"[size=1em]); [size=1em] [size=1em]} [size=1em] [size=1em]private [size=1em]Person(String name, Integer age, Gender gender) { [size=1em] [size=1em]System.out.println([size=1em]"arg constructor"[size=1em]); [size=1em] [size=1em]this[size=1em].name = name; [size=1em] [size=1em]this[size=1em].age = age; [size=1em] [size=1em]this[size=1em].gender = gender; [size=1em] [size=1em]} [size=1em] [size=1em]private [size=1em]Object readResolve() [size=1em]throws [size=1em]ObjectStreamException { [size=1em] [size=1em]return [size=1em]InstanceHolder.instatnce; [size=1em] [size=1em]} [size=1em]}</code> |
1 2 3 | [size=1em]<code [size=1em]class[size=1em]=[size=1em]"hljs delphi"[size=1em]>arg constructor [size=1em][John, [size=1em]31[size=1em], MALE] [size=1em]true[size=1em]</code> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | [size=1em]<code [size=1em]class[size=1em]=[size=1em]"hljs java"[size=1em]> [size=1em]package [size=1em]com.inout; [size=1em] [size=1em]import [size=1em]java.io.Serializable; [size=1em] [size=1em]public [size=1em]class [size=1em]A [size=1em]implements [size=1em]Serializable { [size=1em] [size=1em]private [size=1em]static [size=1em]final [size=1em]long [size=1em]serialVersionUID = 1L; [size=1em] [size=1em]private [size=1em]String name; [size=1em] [size=1em]public [size=1em]String getName() [size=1em] [size=1em]{ [size=1em] [size=1em]return [size=1em]name; [size=1em] [size=1em]} [size=1em] [size=1em]public [size=1em]void [size=1em]setName(String name) [size=1em] [size=1em]{ [size=1em] [size=1em]this[size=1em].name = name; [size=1em] [size=1em]} [size=1em] [size=1em]} [size=1em] [size=1em]package [size=1em]com.inout; [size=1em] [size=1em]import [size=1em]java.io.Serializable; [size=1em] [size=1em]public [size=1em]class [size=1em]A [size=1em]implements [size=1em]Serializable { [size=1em] [size=1em]private [size=1em]static [size=1em]final [size=1em]long [size=1em]serialVersionUID = 2L; [size=1em] [size=1em]private [size=1em]String name; [size=1em] [size=1em]public [size=1em]String getName() [size=1em] [size=1em]{ [size=1em] [size=1em]return [size=1em]name; [size=1em] [size=1em]} [size=1em] [size=1em]public [size=1em]void [size=1em]setName(String name) [size=1em] [size=1em]{ [size=1em] [size=1em]this[size=1em].name = name; [size=1em]</code> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | [size=1em]<code [size=1em]class[size=1em]=[size=1em]"hljs avrasm"[size=1em]>ObjectOutputStream out = [size=1em]new [size=1em]ObjectOutputStream( [size=1em] [size=1em]new [size=1em]FileOutputStream([size=1em]"result.obj"[size=1em])); [size=1em] [size=1em]Test test = [size=1em]new [size=1em]Test(); [size=1em] [size=1em]//试图将对象两次写入文件 [size=1em] [size=1em]out.writeObject(test); [size=1em] [size=1em]out.flush(); [size=1em] [size=1em]System.out.println([size=1em]new [size=1em]File([size=1em]"result.obj"[size=1em]).length()); [size=1em] [size=1em]out.writeObject(test); [size=1em] [size=1em]out.close(); [size=1em] [size=1em]System.out.println([size=1em]new [size=1em]File([size=1em]"result.obj"[size=1em]).length()); [size=1em] [size=1em]ObjectInputStream oin = [size=1em]new [size=1em]ObjectInputStream([size=1em]new [size=1em]FileInputStream( [size=1em] [size=1em]"result.obj"[size=1em])); [size=1em] [size=1em]//从文件依次读出两个文件 [size=1em] [size=1em]Test t1 = (Test) oin.readObject(); [size=1em] [size=1em]Test t2 = (Test) oin.readObject(); [size=1em] [size=1em]oin.close(); [size=1em] [size=1em]//判断两个引用是否指向同一个对象 [size=1em] [size=1em]System.out.println(t1 == t2); [size=1em]/* [size=1em]31 [size=1em]36 [size=1em]true [size=1em]*/ [size=1em]</code> |
欢迎光临 信息发布软件,b2b软件,广告发布软件 (http://postbbs.com/) | Powered by Discuz! X3.2 |