1、什么是对象序列化
2、案例
在JAVA里,只要相应的类实现Serializable接口(java.io.Serializable)就可以进行序列化和反序列化。
待序列化和反序列化的类Ser,
import java.io.Serializable; public class Ser implements Serializable { private String name=""; private int a=0; private static int aa=0; private static String name1=""; public Ser() { // TODO Auto-generated constructor stub this.name="hello"; this.name1="hello1"; this.a=10; this.aa=100; } @Override public String toString() { // TODO Auto-generated method stub return "[name:"+name+" name1:"+name1+" a:"+a+" aa:"+aa+"]"; } public String getName() { return name; } public void setName(String name) { this.name = name; } public static String getName1() { return name1; } public static void setName1(String name1) { Ser.name1 = name1; } } 同一个虚拟机测试类:(就是进行序列化后直接进行反序列化) import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; public class SerTest { public static void main(String[] args) { // TODO Auto-generated method stub Ser ser=new Ser(); try { //对对象进行序列化 ObjectOutputStream objectOutputStream=new ObjectOutputStream(new FileOutputStream("Ser.out")); objectOutputStream.writeObject(ser); objectOutputStream.flush(); objectOutputStream.close(); //读取对象字节码 ObjectInputStream objectInputStream=new ObjectInputStream(new FileInputStream("Ser.out")); Ser object=(Ser)objectInputStream.readObject(); objectInputStream.close(); //显示 System.out.println(object.toString()); object.setName("dadada"); System.out.println(object.toString()); System.out.println(ser.toString()); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }测试结果:
不同虚拟机下进行测试:(开启两个JAVA虚拟机) import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; public class SerTest { public static void main(String[] args) { // TODO Auto-generated method stub //Ser ser=new Ser(); try { // //对对象进行序列化 // ObjectOutputStream objectOutputStream=new ObjectOutputStream(new FileOutputStream("Ser.out")); // objectOutputStream.writeObject(ser); // objectOutputStream.flush(); // objectOutputStream.close(); //读取对象字节码 ObjectInputStream objectInputStream=new ObjectInputStream(new FileInputStream("D:/workSpeace/Eclipse/JavaSE_2/Serilizable/Ser.out")); Ser object=(Ser)objectInputStream.readObject(); objectInputStream.close(); //显示 System.out.println(object.toString()); object.setName("dadada"); System.out.println(object.toString()); //System.out.println(ser.toString()); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }测试结果:
此时必须注意的是,当重新读取被保存的Person对象时,并没有调用Person的任何构造器,看起来就像是直接使用字节将Person对象还原出来的。