2013-12-09 3 views
1

개체를 만들 수없는 이유는 무엇입니까? 하나를 만들려고 할 때마다 나는 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"의 내용을 확인하겠습니까? –

+0

제대로 연재 되었습니까? 'repo' 클래스와 직렬화에 대한 코드를 게시하십시오. –

+0

ObjectInputStream 생성자에서 EOFException을 나타내는 스택 추적을 기반으로 'student.txt'파일이 직렬화 된 직렬화 된 Java 객체라는 가정을해야합니다. 잘못된. 직렬화 부분에 대한 코드가 있습니까? – claymore1977

답변

0

EOFException은 파일 끝에 도달 할 때 throw됩니다. 즉, 전체 파일을 읽었습니다. 따라서 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이 Throw되는 경우는 파일의 끝에 도달했기 때문입니다. 새로운 objectinputstream 인스턴스를 생성 할 때 예외가 throw되지 않습니다 ('readFully' /'readShort'가 throw합니다). – DavidS

0

질문에 대한 이해에서 나는 영업 이익은 아래와 같은 몇 가지 일을하고 가정하고있는이 일을해야한다. 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(); 
     } 
    } 

}