反序列化问题

时间:2015-08-30 22:23:06

标签: java serialization fileinputstream objectinputstream

我的程序中有一个从文件读取的方法,并且我已经与FileInputStream变量和ObjectInputStream变量相关联。但是,我不知道在运行程序时文件中将序列化多少个对象,因此我不知道使用readObject()方法反序列化多少个对象。这是我项目的方法:

public void importResults(File file) throws FileNotFoundException, IOException, ClassNotFoundException {
    TestEntry entry = null;
    try(FileInputStream fis = new FileInputStream(file)) {
        ObjectInputStream ois = new ObjectInputStream(fis);

        tests.clear();

        entry = (TestEntry)ois.readObject();

        tests.add(entry);

        ois.close();
    }
}

变量条目是我将存储从文件反序列化的TestEntry对象的位置。问题是,如果我尝试反序列化太多对象,我会得到一个EOFException。如何让我的程序找出文件中序列化的对象数量,以便我可以反序列化正确的数量?任何帮助将不胜感激!谢谢。

4 个答案:

答案 0 :(得分:1)

我认为没有办法计算序列化了多少个对象,因为一个文件只能容纳一个对象。你可能正在获得EOFException,因为你正在做类似下面的事情。由于文件中只有一个Object,无论您序列化多少,因此从同一个Stream读取两次将导致EOFException。

in.readObject();
in.readObject();

我刚刚测试过,只要您不执行上述操作或类似操作,您就可以从同一个文件中多次读取Object。

我的测试在下面

public static void main(String []args) {
     writeObject(new Integer(333));
     for(int i = 0; i < 9999; i++) {
         Integer i = (Integer)readObject();
         System.out.println(i);//prints 333
     }
}
public static void writeObject(Object obj) {
    try {
        ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("file.dat"));
        out.writeObject(obj);
        out.close();
    }
    catch(IOException e) {}
}
public static Object readObject() {
    Object obj = null;
    try {
    ObjectInputStream in = new ObjectInputStream(new FileInputStream("file.dat"));

        obj = in.readObject();
        in.close();
    }
    catch(IOException e){}
    catch(ClassNotFoundException e){}
    return obj;
}

答案 1 :(得分:1)

也许您可以编写文件中存在的对象数量,并且反序列化首先读取该数字(但是我不确定一个文件是否可以包含多个序列化对象)。

但是,如果您将对象放在序列化的ArrayList中,则可以将ArrayList写入文件,并通过阅读一个对象对其进行反序列化。

我制作了一个测试课A,这个有效:

public class A implements Serializable {
    int a, b, c;
} 

public static ArrayList<A> deserializeObjects(File file) throws FileNotFoundException, IOException, ClassNotFoundException {
    FileInputStream fIn = new FileInputStream(file);
    ObjectInputStream oIn = new ObjectInputStream (fIn);
    ArrayList<A> objects = null;

    // Read array of objects
    if(fIn.available() > 0) {
        objects = (ArrayList<A>) oIn.readObject();
    }

    oIn.close();
    fIn.close();

    return objects;
}

public static void serializeObjects(File file, ArrayList<A> objects) throws IOException {
    FileOutputStream fOut = new FileOutputStream(file);
    ObjectOutputStream oOut = new ObjectOutputStream(fOut);

    // Write the whole arraylist to file
    oOut.writeObject(objects);
    oOut.flush();
    oOut.close();
    fOut.close();
}

public static void main(String args[]) throws IOException, ClassNotFoundException {
    // Make test objects
    A o1 = new A();
    A o2 = new A();

    o1.a = 1; o1.b = 2; o1.c = 3;
    o2.a = 4; o2.b = 5; o2.c = 6;

    ArrayList<A> objects = new ArrayList<A>();
    objects.add(o1);
    objects.add(o2);

    File f = new File("yourFile");
    f.createNewFile();

    serializeObjects(f, objects); // Serialize arraylist
    ArrayList<A> deserialized = deserializeObjects(f); // Deserialize it

    // Success??
    System.out.println("a : " + deserialized.get(0).a + ", b : " + deserialized.get(0).b + ", c : " + deserialized.get(0).c);
    System.out.println("a : " + deserialized.get(1).a + ", b : " + deserialized.get(1).b + ", c : " + deserialized.get(1).c);
}

答案 2 :(得分:1)

只需读取一个循环,直到你得到EOFException,,当没有更多的对象可以读取时,它会被抛出。

答案 3 :(得分:0)

您可以将对象添加到List并序列化/反序列化该列表。

    // entry = (TestEntry)ois.readObject();
    List<TestEntry> entries = (List<TestEntry>)ois.readObject();
相关问题