2017-04-05 37 views
-1

我想寫一個文件上的對象,然後從同一個文件中讀取它 我從一個簡單的文本文件填充對象的arraylist。 我有一個EOFException類當我試圖在文件從文件讀取和寫入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(); 
} 

你讀循環無效。 readObject()在流結束時不返回null,因此作爲循環終止條件進行測試是毫無意義的。

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