首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法创建ObjectInputStream

无法创建ObjectInputStream
EN

Stack Overflow用户
提问于 2013-12-10 02:01:54
回答 2查看 946关注 0票数 1

为什么不能创建ObjectInputStream对象?每次我尝试创建一个时,我都会得到EOFException,但我不知道为什么。有人能帮我吗?下面是我遇到问题的代码和从执行中获得的堆栈跟踪。文件为空。

代码语言:javascript
复制
public void loadFromFileStudent() throws IOException, ClassNotFoundException {
    try{
        InputStream inputStream = new FileInputStream("student.txt");
        System.out.println(inputStream.toString());
        ObjectInputStream objectInputStream;
        objectInputStream = new ObjectInputStream(inputStream);
        System.out.println(objectInputStream.toString());
        this.repo=(Dictionary<Integer, Student>)   objectInputStream.readObject();

        objectInputStream.close();
        inputStream.close();
    }catch (EOFException e){
        e.printStackTrace();;
        //System.out.println(e.getMessage());
    }
}

代码语言:javascript
复制
java.io.FileInputStream@65ddcac5
java.io.EOFException
    at java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInputStream.java:2324)
    at java.io.ObjectInputStream$BlockDataInputStream.readShort(ObjectInputStream.java:2793)
    at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:799)
    at java.io.ObjectInputStream.<init>(ObjectInputStream.java:299)
    at repository.Repository.loadFromFileStudent(Repository.java:94)
    at repository.Repository.<init>(Repository.java:112)
    at utils.DataStructure.createRepository(DataStructure.java:16)
    at controller.Controller.<init>(Controller.java:9)
    at utils.DataStructure.createController(DataStructure.java:20)
    at application.RunMenu.<init>(RunMenu.java:15)
    at application.App.main(App.java:5)
EN

回答 2

Stack Overflow用户

发布于 2013-12-10 02:07:04

当到达文件末尾时抛出EOFException。也就是说,您已经阅读了整个文件。因此,不应该在try语句中关闭流,而应该使用try-with-resources自动关闭它们。

试试像这样简单的东西:

代码语言:javascript
复制
public void loadFromFileStudent() throws IOException, ClassNotFoundException {
    try (InputStream inputStream = new FileInputStream("student.txt");
        ObjectInputStream objectInputStream = new ObjectInputStream(inputStream)) {
        this.repo = (Dictionary<Integer, Student>) objectInputStream.readObject();
    } catch (FileNotFoundException e) {
        System.out.println ("File not found");
    } catch (IOException e) {
        System.out.println ("Error while reading");
    } catch (ClassNotFoundException e) {
        System.out.println ("No class");
    } catch (ClassCastException e) {
        System.out.println ("Could not cast to class");
    }
}

编写也同样简单:

代码语言:javascript
复制
public void writeObject ( Object o ) {
    try (FileOutputStream fos = new FileOutputStream ( this.filename );
        ObjectOutputStream oos = new ObjectOutputStream(fos)) {
        oos.writeObject(o);
        oos.flush();
    } catch (NotSerializableException e) {
        System.out.println ("Object wont be serialized");
        e.printStackTrace();
    } catch (IOException e) {
        System.out.println ("Error while writing to file");
        e.printStackTrace();
    }
}
票数 0
EN

Stack Overflow用户

发布于 2013-12-10 02:26:36

根据我对这个问题的理解,我假设OP正在做一些类似下面的事情,这应该是可行的。可能是OP在写入/读取过程中遗漏了一些东西。希望这能帮你弄清楚。

代码语言:javascript
复制
public class Test2 {
    public static void main(String[] args) {
        Test2 t = new Test2();
        t.create();
        t.read();
    }

    public void create(){
        try{
            ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream("D:\\test\\ab.txt"));
            Student st = new Student("chevs");
            Dictionary<Integer, Student> dict = new Hashtable<Integer, Student>();
            dict.put(1, st);
            os.writeObject(dict);
        }catch(Exception e){
            e.printStackTrace();
        }
    }
    public void read()
    {
        try{
            InputStream inputStream = new FileInputStream("D:\\test\\a.txt");
            System.out.println(inputStream.toString());
            ObjectInputStream objectInputStream;
            objectInputStream = new ObjectInputStream(inputStream);
            System.out.println(objectInputStream.toString());
            private Dictionary<Integer, Student> repo=(Dictionary<Integer, Student>)   objectInputStream.readObject();
            System.out.println(repo.get(1));
            objectInputStream.close();
            inputStream.close();

        }catch (Exception e){
            e.printStackTrace();;
        }
    }

    public class Student  implements Serializable{
        public String name=null;
            public Student(String name){
               this.name=name;
            }
        public String toString() {
            return name.toString();
        }
    }

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20477440

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档