1

把对象以流的方法,写入到文件中保存:写对象(对象的序列化)
对象中包含的不仅仅是字符,使用字节流

ObjectOutputStream(对象序列化流)
ObjectInputStream(对像反序列化流)
image.png

ObjectOutputStream类

对象的序列化流
java.io.ObjectOutputStream extends OutputStream
把对象以流的形式写入到文件中保存

构造方法:

ObjectOutputStream(OutputStream out):创建写入指定OutputStream的ObjectOutputStream
参数:
    OutputStream out:字节输出流

特有成员方法:

void writeObject(Object obj):将指定的对象写入ObjectOutputStream

使用步骤

  1. 创建ObjectOutputStream对象,构造方法传递字节输出流
  2. 使用ObjectOutputStream对象中writeObject方法,把对象写入到文件中
  3. 释放资源

序列化和反序列化时,会抛出NotSerializableException
要实现序列化和反序列化的类必须实现Serializable接口(仅起标记作用)

image.png

public class Person implements Serializable {
    private String name;
    private int age;
    ....
}
    public static void main(String[] args) throws IOException {
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("E:\\A JI\\program\\java\\test-txt\\person.txt"));

        oos.writeObject(new Person("hehe", 18));
        oos.close();
    }

ObjectInputStream类

对象的反序列化流
java.io.ObjectInputStream extends InputStream
把文件中保存的对象,以流的方式读取使用

构造方法:

ObjectInputStream(InputStream In):创建写入指定InputStream的ObjectInputStream
参数:
    InputStream in:字节输入流

特有的成员方法:

Object readObject() 从ObjectInputStream 读取对象

使用步骤

  1. 创建ObjectInputStream对象,构造方法传递字节输入流
  2. 使用ObjectInputStream对象中readObject方法,读取保存对象的文件
  3. 释放资源
  4. 使用读取的对象(打印)

readObject()声明会抛出ClassNotFoundException(class文件找不到异常)

反序列化前提:

**1.类必须实现Serializable接口
2.必须存在类对应的class文件**

    public static void main(String[] args) throws IOException, ClassNotFoundException {
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("E:\\A JI\\program\\java\\test-txt\\person.txt"));

        Object o = ois.readObject();

        ois.close();
        System.out.println(o);
        Person p = (Person)o;
        System.out.println(p.getName() + p.getAge());
    }

output:
Person{name='hehe', age=18}
hehe18

image.png

transient关键字
被transient修饰的成员变量,不能被序列化

image.png

解决方案:
开发者自己定义一个序列号

private static final long serialVersionUID = 1L;

案例

序列化集合

    public static void main(String[] args) throws IOException, ClassNotFoundException {
        //定义存储Person对象的数组
        ArrayList<Person> list = new ArrayList<>();
        list.add(new Person("zz", 18));
        list.add(new Person("qq", 19));
        list.add(new Person("cc", 20));
        //存储Person对象到ArrayList数组中
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("E:\\A JI\\program\\java\\test-txt\\person12.txt"));
        oos.writeObject(list);
        //读取文件中保存的集合
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("E:\\A JI\\program\\java\\test-txt\\person12.txt"));
        Object o = ois.readObject();
        //将Object类型集合转换为ArrayList集合
        ArrayList<Person> list2 = (ArrayList<Person>) o;

        for (Person person : list2) {
            System.out.println(person.getName() + " " + person.getAge());
        }
    }
    
    
output:
zz 18
qq 19
cc 20

打印流

java.io.PrintStream extends OutputStream

特点:

  1. 只负责数据输出,不负责数据的读取
  2. 永远不会抛出IOException
  3. 特有方法:print,println

构造方法:

PrintStream(File file):输出目的地是一个文件
PrintStream(OutputStream out):...是一个字节输出流
PrintStream(String fileName):...是一个文件路径

如果使用继承自父类的write方法写数据,查数据会查询编码表
如果使用自身特有的print/println方法写数据,写得数据原样输出

image.png
image.png


waikiki
4 声望2 粉丝