2013-12-09 51 views
1

为什么我无法创建ObjectInputStream对象?每次我尝试创建一个我得到EOFException,我不明白为什么。有人能帮我吗? 下面是我遇到问题的代码以及从执行中获得的堆栈跟踪。该文件是空的。无法创建ObjectInputStream

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

[email protected] 
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) 
+1

我会检查“student.txt”的内容,是否为空? – 2013-12-09 18:06:22

+0

您是否已正确序列化?发布代码以用于'repo'类和序列化 –

+0

根据显示EOFException来自ObjectInputStream构造函数的堆栈跟踪,我必须假定文件'student.txt'是序列化的序列化Java对象错误。 你有序列化部分的代码吗? – claymore1977

回答

0

EOFException在达到文件结束时被抛出。也就是说,你已经阅读了整个文件。因此,您不应在try声明中关闭您的流,而应使用try-with-resources自动关闭它们。

尝试一些简单的像这样:

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

写作也同样简单:

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

与手头的问题无关 – radai

+0

怎么回事? EOFException被抛出的情况是因为到达了文件的末尾。当创建一个新的objectinputstream实例时,抛出异常('readFully' /'readShort'不会抛出它们)。 – DavidS

0

从我这个问题的理解我认为OP做一些事情像下面,而哪些作品。可能是OP会在写作/阅读过程中漏掉一些东西。希望这有助于弄清楚。

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

}