从文件中读取可序列化对象

时间:2014-01-07 06:54:42

标签: java file deserialization

 List ll=new LinkedList();
 Student temp;

 int size = obj.readInt();
 System.out.println(size);
 for (int i = 0; i <size; ++i) {
        ll.add((Student) obj.readObject());
 }

 obj.close();
     System.out.println(ll);
 }

它导致运行时异常为
    "Exception in thread "main" java.io.EOFException

at java.io.DataInputStream.readInt(Unknown Source)
at java.io.ObjectInputStream$BlockDataInputStream.readInt(Unknown Source)
at java.io.ObjectInputStream.readInt(Unknown Source)
at p1.DeSerializeDemo1.main(DeSerializeDemo1.java:18)"

请给我解决方案。

4 个答案:

答案 0 :(得分:0)

只需处理Exception

即可
try{
      int size = obj.readInt();
 }catch(EOFException ex){}

答案 1 :(得分:0)

EOFException中的

ObjectInputStream.readInt表示当前位置后文件中剩余的字节少于4个字节。

答案 2 :(得分:0)

为什么不创建一个类并保存/加载整个对象(该类需要实现Serializable(并且所有对象也使用该类)),然后使用您可以强制转换的对象获取所需的信息之后:

public static Object load(String path) throws FileNotFoundException, Exception {
    try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(path))) {
        final Object result = ois.readObject();
        ois.close();
        return result;
    }
}

public static void save(Object obj, String path) throws Exception {
    try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(path))) {
        oos.writeObject(obj);
        oos.flush();
        oos.close();
    }
}

答案 3 :(得分:0)

异常原因是您在编写对象之前没有调用ObjectOutputStream.writeInt(int)。据我所知,您正在尝试存储存储在文件中的对象数量。所以,你应该这样做:obj.writeInt(2);

public class Test implements Serializable {

    private static final long serialVersionUID = 243705916609512381L;

    private String name;

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }
}

public static void main(String[] args) {
        Test s = new Test();
        s.setName("Test");
        Test s1 = new Test();
        s1.setName("Test 2");
        ObjectOutputStream oos = null;
        FileOutputStream fileOutputStream = null;
        FileInputStream fileInputStream = null;
        ObjectInputStream ois = null;
        try {
            fileOutputStream = new FileOutputStream("C:\\test.txt");
            oos = new ObjectOutputStream(fileOutputStream);
            oos.writeInt(2);
            oos.writeObject(s1);
            oos.writeObject(s);
            oos.flush();
            oos.close();
            fileOutputStream.close();

            fileInputStream = new FileInputStream("C:\\test.txt");
            ois = new ObjectInputStream(fileInputStream);
            int readInt = ois.readInt();
            System.out.println("Read int " + readInt);
            Test readObject = (Test) ois.readObject();
            System.out.println(readObject.getName());
            Test readObject2 = (Test) ois.readObject();
            System.out.println(readObject2.getName());
            ois.close();
            fileInputStream.close();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            // close everything
        }
    }