2017-04-05 12 views
-1

파일에 개체를 쓰고 동일한 파일에서 개체를 읽으려고합니다. 간단한 텍스트 파일에서 개체의 arraylist를 채 웁니다. 내가 File From Java에서 Obj를 읽고 씁니다. EOFException

public class CaricaTestoScriviOggetti_Copia { 

    public static void main(String[] args) { 
     File fileIn = new File("src/Lista.txt"); 
     Scanner input = null; 
     try { 
      input = new Scanner(fileIn); 
     } catch (FileNotFoundException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

     ArrayList<Persona> persone = new ArrayList<Persona>(); 
     ArrayList<Persona> maggiorenni = new ArrayList<Persona>(); 
     String nome = null; 
     String cognome = null; 
     int eta = 0; 
     while (input.hasNext()) { 
      nome = input.next(); 
      cognome = input.next(); 
      eta = input.nextInt(); 
      if (input.hasNext("\n")) 
       input.nextLine(); 
      persone.add(new Persona(nome, cognome, eta)); 
     } 

     for (Persona p : persone) { 
      System.out.println(p); 
      if (p.getEta() > 17) 
       maggiorenni.add(p); 
     } 

     input.close(); 

     FileOutputStream fos = null; 
     ObjectOutputStream oos = null; 
     try { 
      fos = new FileOutputStream("src/ListaObj.dat"); 
     } catch (FileNotFoundException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     try { 
      oos = new ObjectOutputStream(fos); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

     for (Persona p : maggiorenni) { 
      try { 
       oos.writeObject(p); 
       oos.flush(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } 
     // END FOR 

     try { 
      fos.close(); 
      oos.close(); 
     } catch (IOException e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } 

     FileOutputStream fos2 = null; 
     ObjectOutputStream oos2 = null; 
     try { 
      fos2 = new FileOutputStream("src/Oggetto.dat"); 
     } catch (FileNotFoundException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     try { 
      oos2 = new ObjectOutputStream(fos); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

     try { 
      oos2.writeObject(maggiorenni.get(1)); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

     try { 
      fos2.close(); 
      oos2.close(); 
     } catch (IOException e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } 

     FileInputStream fis = null; 
     ObjectInputStream ois = null; 
     try { 
      fis = new FileInputStream("src/Oggetto.dat"); 
     } catch (FileNotFoundException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     try { 
      ois = new ObjectInputStream(fis); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

     Persona catched = null; 

     try { 
      catched = (Persona) ois.readObject(); 
     } catch (ClassNotFoundException | IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

     // END FOR 

     try { 
      fis.close(); 
      ois.close(); 
     } catch (IOException e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } 

     System.out.println("<------------Oggetto Letto------------>"); 
     System.out.println(catched); 

     // END MAIN 
    } 
    // END CLASS 
} 
파일

+1

'파일에 쓸 때 EOFException이 발생합니다.' 아니야. 파일에서 * 읽으려고 할 때'EOFException'이 생기고 그 이유는 파일에 * 아무것도 없다는 것입니다. 주의 코드를 작성하지 마십시오. 이전의'try' 블록에서 코드의 성공 여부에 의존하는 코드는 그'try' 블록 안에 있어야합니다. – EJP

+0

죄송합니다. 코드의 잘못된 부분을 게시하고 있습니다. 이 게시물을 삭제하고 올바른 코드 부분을 작성해야한다고 생각하십니까? – Apples1986

+0

이것은이 오류를주는 코드입니다. – Apples1986

답변

0
p = (Persona) ois.readObject(); 
k++; 
while (p != null) { 
    persone.add(p); 
    p = (Persona) ois.readObject(); 
} 

귀하의 읽기 루프가 유효하지 않은 쓰기를 시도하고있을 때 나는 예외 : EOFException 있습니다. readObject()은 스트림 끝 부분에서 null을 반환하지 않으므로이를 루프 종료 조건으로 테스트하는 것은 의미가 없습니다.

for (;;) { 
    try { 
     p = (Persona) ois.readObject(); 
     k++; 
     persone.add(p); 
    } catch (EOFException exc) { 
     break; 
    } 
}