2013-12-09 53 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(); 
     } 
    } 

}