用于从流中读取对象的操作流 ObjectInputStream 称为反序列化流
用于向流中写入对象的操作流 ObjectOutputStream 称为序列化流
特点:用于操作对象。可以将对象写入文件中,也可以从文件中读取对象
对象序列化流ObjectOutputStream 将Java对象的基本数据类型和图形写入OutputStream。可以使用ObjectInputStream读取(重构)对象,通过在流中使用文件可以实现对象的持久存储。
注意:只能将支持java.io.Serializable接口的对象写入流中
构造方法: ObjectOutputStream(OutputStream out) 创建写入指定OutputStream的ObjectOutputStream。
方法:writeObject(Object obj) 将指定的对象写入ObjectOutputStream
代码演示:
public class ObjectStreamDemo { public static void main(String[] args) throws IOException, ClassNotFoundException { writeObj();//对象的序列化。 } public static void writeObj() throws IOException { //1,明确存储对象的文件。 FileOutputStream fos = new FileOutputStream("tempfile\\obj.object"); //2,给操作文件对象加入写入对象功能。 ObjectOutputStream oos = new ObjectOutputStream(fos); //3,调用了写入对象的方法。 oos.writeObject(new Person("wangcai",20)); //关闭资源。 oos.close(); } }
Person类
public class Person implements Serializable { private String name; private int age; public Person() { super(); } public Person(String name, int age) { super(); this.name = name; this.age = age; } 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 "Person [name=" + name + ", age=" + age + "]"; } }
对象反序列化流ObjectInputStream 对以前使用ObjectOutputStream写入的基本数据和对象进行反序列化,支持java.io.Serializable接口的对象才能从流读取
构造方法: ObjectInputStream(InputStream in) 创建从指定InputStream读取的ObjectInputStream
方法: readObject() 从ObjectInputStream读取对象
代码演示:
public class ObjectStreamDemo { public static void main(String[] args) throws IOException, ClassNotFoundException { readObj();//对象的反序列化。 } public static void readObj() throws IOException, ClassNotFoundException { //1,定义流对象关联存储了对象文件。 FileInputStream fis = new FileInputStream("tempfile\\obj.object"); //2,建立用于读取对象的功能对象。 ObjectInputStream ois = new ObjectInputStream(fis); Person obj = (Person)ois.readObject(); System.out.println(obj.toString()); } }
序列化接口 当一个对象要被序列化,这个对象所属的类必须实现Serializable接口,否则会发生异常NotSerializableException。同时当反序列化对象时,如果对象所属的class文件在序列化之后进行的修改,那么进行反序列化也会发生异常InvalidClassException。发生这个异常的原因如下:
- 该类的序列版本号与从流中读取的类描述符的版本号不匹配
- 该类包含未知数据类型
- 该类没有可访问的无参数构造方法
Serializable接口,该接口给需要序列化的类,提供了一个序列版本号。SerialVersionUID,该版本号的目的在于验证序列化的对象和对应类是否版本匹配。
瞬态关键字transient
当一个类的对象需要被序列化时,某些属性不需要被序列化,这时不需要序列化的属性可以使用关键字transient修饰。只要被transient修饰了,序列化时这个属性就不会被序列化了。
同时静态修饰也不会被序列化,因为序列化是把对象数据进行持久化存储,而静态的属于类加载时的数据,不会被序列化。
打印流 打印流有添加输出数据的功能,使它们能够方便的打印各种数据值表示形式。
根据流的分类可分为:字节打印流PrintStream和字符打印流PrintWriter
方法: print(String str):输出任意类型的数据
println(String str):输出任意类型的数据,自动写入换行操作
代码演示:
/* * 需求:把指定的数据,写入到printFile.txt文件中 */ public class PrintWriterDemo { public static void main(String[] args) throws IOException { //创建流 //PrintWriter out = new PrintWriter(new FileWriter("printFile.txt")); PrintWriter out = new PrintWriter("printFile.txt"); //2,写数据 for (int i=0; i<5; i++) { out.println("helloWorld"); } //3,关闭流 out.close(); } }
打印流可以通过构造方法,完成文件数据的自动刷新功能
构造方法: 开启文件自动刷新写入功能
Public PrintWriter(OutputStream out, boolean autoFlush)
Public PrintWriter(Writer out, boolean autoFlush)
代码演示:
public class PrintWriterDemo2 { public static void main(String[] args) throws IOException { //创建流 PrintWriter out = new PrintWriter(new FileWriter("printFile.txt"), true); //2,写数据 for (int i=0; i<5; i++) { out.println("helloWorld"); } //3,关闭流 out.close(); } }