简体   繁体   中英

reading the serializable objects from file

 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);
 }

it causes the run time exception as
"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)"

Please give me solution for this.

Just handle the Exception

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

ObjectInputStream.readInt EOFException表示文件在当前位置之后剩余不到4个字节。

Why don't you create a class and save/load the entire object (that class needs to implement Serializable though (and all objects used it that class too)), then get the information you want using that object you can cast it afterwards:

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();
    }
}

Exception cause is that you are not calling ObjectOutputStream.writeInt(int) before writing objects. As far as I understand you are trying to store number of objects, which is stored in the file. So, you should do it like this: 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
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM