ObjectOutputStream中,只调用readObject读取序列化的文件的第一个对象 [英] ObjectOutputStream, readObject only reads first object from serialized file

查看:138
本文介绍了ObjectOutputStream中,只调用readObject读取序列化的文件的第一个对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有对象的ArrayList,我想将它们存储到文件中,我也想从文件中读取他们ArrayList的。我可以将它们成功地写入使用writeObject方法的文件,但是从文件中读取到ArrayList的时候,我只能读取第一个对象。这里是我的code从序列化文件中读取

 公共无效loadFromFile()抛出IOException异常,ClassNotFoundException异常{
        FIS的FileInputStream =新的FileInputStream(文件);
        ObjectInputStream的OIS =新的ObjectInputStream(FIS);
        myStudentList =(ArrayList的<的Student GT;)ois.readObject();
}

编辑:

这是code写列表到文件中。

 公共无效的SaveToFile(ArrayList的<的Student GT;清单)抛出IOException
        ObjectOutputStream的出= NULL;
        如果OUT =新的ObjectOutputStream(新的FileOutputStream(文件))(file.exists()!);
        否则出=新AppendableObjectOutputStream(新的FileOutputStream(文件,真正的));
        out.writeObject(名单);
}

我班休息是

 公共类学生实现Serializable {
    字符串名称;
    串姓;
    INT ID;
    公众的ArrayList<的Student GT; myStudentList =新的ArrayList<的Student GT;();
    档案文件=新的文件(SRC /文件/ students.txt);
    公益助学(字符串namex,字符串surnamex,INT IDX){
        this.name = namex;
        this.surname = surnamex;
        this.ID = IDX;
    }    公益助学(){}    // getter和setter
    公共无效的add(){        扫描仪输入=新的扫描仪(System.in);
        的System.out.println(名称);
        字符串名称= input.nextLine();
        的System.out.println(姓);
        字符串姓= input.nextLine();
        的System.out.println(ID);
        INT ID = input.nextInt();
        Ogrenci studenttemp =新Ogrenci(姓名,身份证);
        myOgrenciList.add(studenttemp);
        尝试{
            的SaveToFile(myOgrenciList,真);
        }
        赶上(IOException异常五){
            e.printStackTrace();
        }
    }


解决方案

好了,所以你在每次存放学生的整个列表,当新的学生进来,所以basicly你的文件保存为:


  1. 一个学生名单

  2. 有两个学生,其中第一个列表

  3. 3 studens列表

  4. 等等等等。

我知道你可能认为它必将谱写渐进的方式只有新学生,但你错了这里

您应该先,而添加你想存储所有的学生,进入名单。然后完整列表保存到文件,就像你在这样做。

现在,当你会从filre读书,第一个的readObject 将返回列表一号 - 这就是为什么你只有一个学生越来越名单。第二次读取会给你列表二号等。

所以,你保存你的数据,你要么必须:


  1. 创建完整列表containig n个学生,一旦存放ITO文件

  2. 请不要使用名单,但店内的学生直接到文件

要读回:


  1. 的readObject 一次,所以你会得到列表<学生>

  2. 阅读同学们一个个从多次调用该文件的readObject

I have an ArrayList of Objects and i want to store them into the file and also i want to read them from the file to ArrayList. I can successfully write them into the file using writeObject method but when reading from the file to ArrayList, i can only read first object. Here is my code for reading from serialized file

 public void loadFromFile() throws IOException, ClassNotFoundException {
        FileInputStream fis = new FileInputStream(file);
        ObjectInputStream ois = new ObjectInputStream(fis);
        myStudentList = (ArrayList<Student>) ois.readObject();
}

EDIT:

This is the code for writing list into the file.

 public void saveToFile(ArrayList<Student> list) throws IOException {
        ObjectOutputStream out = null;
        if (!file.exists ()) out = new ObjectOutputStream (new FileOutputStream (file));
        else out = new AppendableObjectOutputStream (new FileOutputStream (file, true));
        out.writeObject(list);
}

Rest of my class is

public class Student implements Serializable {
    String name;
    String surname;
    int ID;
    public ArrayList<Student> myStudentList = new ArrayList<Student>();
    File file = new File("src/files/students.txt");


    public Student(String namex, String surnamex, int IDx) {
        this.name = namex;
        this.surname = surnamex;
        this.ID = IDx;
    }

    public Student(){}

    //Getters and Setters


    public void add() {

        Scanner input = new Scanner(System.in);


        System.out.println("name");
        String name = input.nextLine();
        System.out.println("surname");
        String surname = input.nextLine();
        System.out.println("ID");
        int ID = input.nextInt();
        Ogrenci studenttemp = new Ogrenci(name, surname, ID);
        myOgrenciList.add(studenttemp);
        try {
            saveToFile(myOgrenciList, true);
        }
        catch (IOException e){
            e.printStackTrace();
        }


    }

解决方案

Ok so you are storing whole list of students every time when new student comes in, so basicly what your file is keeping is:

  1. List with one student
  2. List with two students including the first one
  3. List of 3 studens
  4. and so on and so on.

I know you are probably thought it will write only new students in incremental fashion, but you were wrong here .

You should rather add all students you want to store, into the list first. And then store complete list into the file , just like you are doing it.

Now, when you will be reading from the filre, first readObject will return you the list no.1 - that is why you are getting list with only one student. Second read would give you list no.2 and so on.

So you save your data you either have to:

  1. Create complete list containig N students and store it once ito the file
  2. Do not use list, but store students directly to the file

To read it back:

  1. readObject once, so you will get List<Students>
  2. Read students one by one from the file by multiple calls to readObject

这篇关于ObjectOutputStream中,只调用readObject读取序列化的文件的第一个对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆