IO流之对象流(ObjectStream)

本文介绍Java中对象流的基本使用方法,包括ObjectOutputStream和ObjectInputStream的创建及使用,如何通过对象流进行对象的序列化和反序列化,并展示了如何处理常见的异常如NotSerializableException和EOFException。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package IOliu.ObjectStream;

import java.io.*;

/**
 * 对象操作流:可以用于读写对象
 *      ObjectOutStreadm;
 *            writeObject
 *              ObjectOutputStream(OutputStream out)
 *              创建写入指定 OutputStream 的 ObjectOutputStream。
 *     ObjectInputStream
 *            readObject
 *            ObjectInputStream(InputStream in)
              创建从指定 InputStream 读取的 ObjectInputStream。
 *
 *
 *    注意事项:
 *                 使用对象输出流写出对象,只能使用对象输入流来读取对象
 *                 只能将支持java.io.Serializable接口的对象写入流中
 *
 *                 使用对象输出流和读对象输入流写对象
 *                 Exception in thread "main" java.io.NotSerializableException: IOliu.ObjectStream.Student
                   Serializable:序列号,是一个标识接口,只有标识作用,没有方法
 *                               当一个类的对象需要IO流进行读写的时候,这个类必须实现该接口
 *
 *
 *                 EOFException 当输入过程中意外到达文件或流的末尾时,抛出此异常
 *                 如何解决这个异常
 *                   A 捕获异常
 *                   B  对象集合 只读一次
 *
 *
 * */
public class ObjectOutStreadmDemo {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
////        ObjectOutStreadm;
//        ObjectInputStream
         method1();
         ObjectInputStream ois=new ObjectInputStream(new FileInputStream("a.txt"));

//        Object o1 = ois.readObject();
//        System.out.println(o1);
//
//        Object o2 = ois.readObject();
//        System.out.println(o2);
//
//        Object o3 = ois.readObject();
//        System.out.println(o3);
        try {
              while (true){
                  Object o1 = ois.readObject();
                  System.out.println(o1);
              }
        }catch (EOFException e){
            System.out.println("文件末尾");
        }
        ois.close();

    }
    public static void method1() throws IOException {
        //创建对象输出流的对象
//        FileInputStream fis=new FileInputStream("F:\\untitledxuexi\\fr.txt");
//        ObjectInputStream oss=new ObjectInputStream(fis);
        ObjectOutputStream oss=new ObjectOutputStream(new FileOutputStream("a.txt"));
        Student t1=new Student("zhangsan",16);
        Student t2=new Student("lisi",18);
        oss.writeObject(t1);
        oss.writeObject(t2);
//        oss.writeOb
        oss.close();
    }
}

删除线格式

package IOliu.ObjectStream;

import java.io.*;
import java.lang.reflect.Array;
import java.util.ArrayList;
/**
 *
 *  EOFException 当输入过程中意外到达文件或流的末尾时,抛出此异常
 *                 如何解决这个异常
 *                   A 捕获异常
 *                   B  对象集合 只读一次  这个Demo采取这个方法
 *
 */
public class ObjectDemo2 {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
//       method1();

       method2();

    }
    public static void method2() throws IOException, ClassNotFoundException {
        ObjectInputStream ois=new ObjectInputStream(new FileInputStream("a.txt"));
        Object o = ois.readObject();
        System.out.println(o);
        //向下还原
        ArrayList<Student> list=(ArrayList<Student>) o;
        System.out.println(list.get(0));
        System.out.println(list);
        ois.close();
    }
    public static void method1() throws IOException {
        ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("a.txt"));
        ArrayList<Student> arrayList=new ArrayList <>();
        arrayList.add(new Student("wangwu",28));
        arrayList.add(new Student("zhaoliu",38));

        oos.writeObject(arrayList);
        oos.close();
    }
}

删除线格式
删除线格式

package IOliu.ObjectStream;

import java.io.Serializable;
/**
 * InvalidClassException
 *  该类的序列版本号与从流中读取的类描述符的版本号不匹配
    该类包含未知数据类型
    该类没有可访问的无参数构造方法
 *    当我们用对象输入流之后,类会产生一个序列号  文件会记住这个序列号,当我们改变类之后 序列号会发生改变
 *    所以会产生这个异常InvalidClassException
 *    解决方法: 生成一个序列号
 *
 * */

public class Student implements Serializable {


//    private static final long serialVersionUID = -8761391123951334202L;
    String name;
      int age;
      int g;
//    transient  int num;   transient 可以保护变量不被读取到
    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值