Java---IO流序列化

★ 序列化 将一个对象存放到某种类型的永久存储器上称为保持。如果一个对象可以被存放到磁盘或磁带上,或者可以发送到另外一台机器并存放到存储器或磁盘上,那么这个对象就被称为可保持的。(在java中,序列化、持久化、串行化是一个概念。) java.io.Serializable接口没有任何方法,它只作为一个“标记者”,用来表明实现了这个接口的类可以考虑串行化。类中没有实现Serializable的对象不能保存或恢复它们的状态。 ★ 对象图 当一个对象被串行化时,只有对象的数据被保存;方法和构造函数不属于串行化流。如果一个数据变量是一个对象,那么这个对象的数据成员也会被串行化。树或者对象数据的结构,包括这些子对象,构成了对象图。 ★ 瞬时 transient 防止对象的属性被序列化。


演示序列化时创建的值对象

package cn.hncu.io.demo2.serial; import java.io.Serializable; public class MyDate implements Serializable { PRivate static final long serialVersionUID = 1L; private int year; private int month; private int day; public MyDate() { super(); } public MyDate(int year, int month, int day) { super(); this.year = year; this.month = month; this.day = day; } public int getYear() { return year; } public void setYear(int year) { this.year = year; } public int getMonth() { return month; } public void setMonth(int month) { this.month = month; } public int getDay() { return day; } public void setDay(int day) { this.day = day; } @Override public String toString() { return year+"年"+month+"月"+day+"日"; } } package cn.hncu.io.demo2.serial; import java.io.Serializable; //在对一个类进行保存或者读取(计算机硬盘或者本地文件)的时候该类必须实现可序列化接口----及该类必须序列化!!! public class Person implements Serializable { private static final long serialVersionUID = 1L; private String name; private int age; private MyDate birth; /* * 这里将解决一个问题---如果想将一个静态变量序列化的情况(我们可以用一个非静态变量来接收静态变量的赋值),然后输出时用静态变量来进行输出即可达到我们想要的结果!!! */ private static int count = 0;//这里存在一个知识点---静态变量是不够被序列化的!! //private transient int num;//这里有一个知识点---如果某个变量被transient修饰的话,该变量也不能够被序列化! private int num; public Person() { num = ++count; } public Person(String name, int age, MyDate birth) { this(); this.name = name; this.age = age; this.birth = birth; } 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; } @Override public String toString() { return num+", "+name+", "+age+", "+birth; } }

进行演示的主函数类

package cn.hncu.io.demo2.serial; import java.io.File; 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 Serial {//对于读写文件而言,只有读文件时才需要判断文件是否存在或者路径是否有问题,写文件时只需要判断文件路径是否正确即可!!!! public static void main(String[] args) { writeDemo(); readDemo(); } public static void readDemo(){ File file = new File("d:\\a\\files\\person.txt"); if(!file.exists()){ System.out.println("文件不存在!"); return; } try { ObjectInputStream in = new ObjectInputStream(new FileInputStream(file)); while(true){//知识点:这个与对于读写int类型的数据来讲是不同的地方!!! Person p = null; try { p = (Person) in.readObject(); } catch (Exception e) { System.out.println("文件已经读取完毕!"); break; } System.out.println(p); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public static void writeDemo(){ File file = new File("d:/a/files/person.txt"); try { ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(file)); out.writeObject(new Person("Jack",25,new MyDate(1995,12,1))); out.writeObject(new Person("张三爱李四",26,new MyDate(2010,2,10))); out.writeObject(new Person("Rose喜欢玫瑰",21,new MyDate(2015,7,15))); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }