2015-08-30 131 views
1

我的程序中有一个方法从文件中读取,并且我已经与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。如何让我的程序弄清楚文件中有多少对象被序列化,这样我可以反序列化正确的数量?任何帮助将不胜感激!谢谢。

+0

你可以发布你得到的异常的堆栈跟踪吗? –

+0

刚刚从readObject捕获EOFException – krzydyn

回答

1

在循环刚看完,直到你得到EOFException,时,有没有更多的对象读取一个抛出。

+0

谢谢,我认为一旦抛出异常,程序就会完全停止。 – James

+0

我建议你看看语言教程。 – EJP

1

我不认为有一种方法可以计算有多少对象被序列化,因为一个文件只能容纳一个对象。您可能正在接受EOFException,因为您正在进行如下操作。由于文件中只有一个对象,无论你有多少序列化,从同一个流读两次将导致EOFException。

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

我只是测试,只要你可以阅读对象的任何数量的来自同一文件倍,你不这样做上述或类似的东西。

我的测试低于

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

也许你可以编写存在于文件和反序列化对象的数量第一次读到这个数字(不过我不知道,如果一个文件可以包含多个序列化对象)。

但是,如果你把你的对象在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); 
} 
0

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

// entry = (TestEntry)ois.readObject(); 
    List<TestEntry> entries = (List<TestEntry>)ois.readObject();