Java使用对象流读取文件的问题

来源:互联网 发布:手机内窥镜软件 编辑:程序博客网 时间:2024/06/10 07:22

把对象进行持久化(把对象存到本地)要用到对象流进行处理,在用对象流处理过程中,在写对象和读对象时候遇到了很多问题,分享一下。
我们处理对象数据的时候不可能只处理一个对象,在系统运行的时候,可能产生的对象数量是随机的,对于向文件中写入对象数据没有什么影响,只需要向文件中写入正确的对象即可,但是从文件中读取对象操作的时候就需要我们进行判断结束标志在哪里,什么时候结束读操作,如果不能判断在哪里结束,程序就会报错,抛出异常。

Student类:

class Student implements Serializable{    public int id;    public String name;    public String sex;    public Student(int id, String name, String sex) {        this.id = id;        this.name = name;        this.sex = sex;    }    @Override    public String toString() {        return "Student{" +                "id=" + id +                ", name='" + name + '\'' +                ", sex='" + sex + '\'' +                '}';    }}

一、

在对一个对象进行处理的时候,我们不需要关心哪里结束,注意在最后要清空缓冲区和关闭资源

//读取对象数据,保存到本地文件中public static void read() {        //声明一个文件(创建文件)        File file = null;        //声明文件输出字节流        FileOutputStream fos = null;        //声明对象处理流        ObjectOutputStream oos = null;        try {            file = new File("E:\\a.txt");            fos = new FileOutputStream(file);            oos = new ObjectOutputStream(fos);            //向文件中写入对象的数据            oos.writeObject(new Student(1002,"ll","女"));            //清空缓冲区            oos.flush();        } catch (FileNotFoundException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        }finally {            try {                //关闭资源                fos.close();                oos.close();            } catch (IOException e) {                e.printStackTrace();            }        }    }


//从文件中把对象数据读取出来打印public static void write() {        File file = null;        FileInputStream fis = null;        ObjectInputStream ois = null;        try {            file = new File("E:\\a.txt");            fis = new FileInputStream(file);            ois = new ObjectInputStream(fis);            Student s = (Student)ois.readObject();            System.out.println(s);        } catch (FileNotFoundException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        } catch (ClassNotFoundException e) {            e.printStackTrace();        } finally {            try {                fis.close();                ois.close();            } catch (IOException e) {                e.printStackTrace();            }        }    }

二、

当创建的对象数量不确定的时候,加入我们创建了两个Student对象,并写入文件中

 public static void read() {        //声明一个文件(创建文件)        File file = null;        //声明文件输出字节流        FileOutputStream fos = null;        //声明对象处理流        ObjectOutputStream oos = null;        try {            file = new File("E:\\a.txt");            fos = new FileOutputStream(file);            oos = new ObjectOutputStream(fos);            oos.writeObject(new Student(1001,"llli","女"));            oos.writeObject(new Student(1002,"ll","女"));            //清空缓冲区            oos.flush();        } catch (FileNotFoundException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        }finally {            try {                //关闭资源                fos.close();                oos.close();            } catch (IOException e) {                e.printStackTrace();            }        }    }

如果我们知道文件中有两个对象的数据,我们可以读取两次,调用两次读取方法,分别读取s和s1

 public static void write() {        File file = null;        FileInputStream fis = null;        ObjectInputStream ois = null;        try {            file = new File("E:\\a.txt");            fis = new FileInputStream(file);            ois = new ObjectInputStream(fis);                     Student s = (Student)ois.readObject();            System.out.println(s);            Student s1 = (Student)ois.readObject();            System.out.println(s1);        } catch (FileNotFoundException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        } catch (ClassNotFoundException e) {            e.printStackTrace();        } finally {            try {                fis.close();                ois.close();            } catch (IOException e) {                e.printStackTrace();            }        }    }

但是往往我们不知道系统中保存了几个对象,再说假如知道了对象的数量,这样的代码也是很不简洁的,几百个对象我们就要写几百个调用readObject()方法的语句,这时候我们想到了用while循环,当判断文件中没有对象数据的时候结束访问,把上面调用两次readObject()方法的语句换成下面这样,放在while循环中

Object obj = null;while ((obj=ois.readObject()) != null) {      System.out.println(obj);}

程序运行出错了,程序可以读取到第一个对象,但是当遇到第二个对象的时候,程序再向后执行不能判断结束标志在哪里抛出EOFException


既然没有结束标志我们在文件中加入一个结束标志,我们在之前的read()方法中,向文件中写入对象的时候最后加上写入一个null

oos.writeObject(new Student(1001,"llli","女"));oos.writeObject(new Student(1002,"ll","女"));oos.writeObject(null);

这样程序在执行的时候,遇到这个null的时候就知道结束了,可以正确的操作