2011-10-19 23 views
4

我有一個文件,其中包含XYZ類的多個序列化對象。在序列化時,每個XYZ對象都附加到文件中。從Java中的文件反序列化對象

現在我需要從文件中讀取每個對象,並且只能讀取第一個對象。

任何想法如何從文件中讀取每個對象並最終將其存儲到List中?

+0

請問您能提供一個能說明問題的代碼片段嗎? – alf

+0

二進制或XML序列化? – spork

+0

是否所有對象都寫在單個會話中(只有一個ObjectOutputStream實例),還是有多個會話(爲每個會話創建,使用和關閉一個ObjectOutputStream)? –

回答

9

嘗試以下操作:

List<Object> results = new ArrayList<Object>(); 
    FileInputStream fis = new FileInputStream("cool_file.tmp"); 
    ObjectInputStream ois = new ObjectInputStream(fis); 

    try { 
     while (true) { 
      results.add(ois.readObject()); 
     } 
    } catch (OptionalDataException e) { 
     if (!e.eof) throw e; 
    } finally { 
     ois.close(); 
    } 

湯姆的精彩評論跟進,多解ObjectOutputStream s就,

public static final String FILENAME = "cool_file.tmp"; 

public static void main(String[] args) throws IOException, ClassNotFoundException { 
    String test = "This will work if the objects were written with a single ObjectOutputStream. " + 
      "If several ObjectOutputStreams were used to write to the same file in succession, " + 
      "it will not. – Tom Anderson 4 mins ago"; 

    FileOutputStream fos = null; 
    try { 
     fos = new FileOutputStream(FILENAME); 
     for (String s : test.split("\\s+")) { 
      ObjectOutputStream oos = new ObjectOutputStream(fos); 
      oos.writeObject(s); 
     } 
    } finally { 
     if (fos != null) 
      fos.close(); 
    } 

    List<Object> results = new ArrayList<Object>(); 

    FileInputStream fis = null; 
    try { 
     fis = new FileInputStream(FILENAME); 
     while (true) { 
      ObjectInputStream ois = new ObjectInputStream(fis); 
      results.add(ois.readObject()); 
     } 
    } catch (EOFException ignored) { 
     // as expected 
    } finally { 
     if (fis != null) 
      fis.close(); 
    } 
    System.out.println("results = " + results); 
} 
+0

調用results.add(ois.readObject()); 在一個循環中 –

+4

如果這些對象是用一個'ObjectOutputStream'編寫的,那麼這將起作用。如果多個'ObjectOutputStream'被用來連續寫入同一個文件,它就不會。 –

+0

@ RockyTriton well spotted :)是的,已經修復了,謝謝! – alf

1

您不能追加ObjectOutputStreams到一個文件中。它們包含標題以及您編寫的對象。修改你的技巧。

此外你的EOF檢測是錯誤的。你應該單獨捕獲EOFException。 OptionalDataException意味着完全不同的東西。

0

這對我的作品

System.out.println("Nombre del archivo ?"); 
        nArchivo= sc.next(); 
        sc.nextLine(); 
        arreglo=new ArrayList<SuperHeroe>(); 

        try{ 

         FileInputStream fileIn = new FileInputStream(nArchivo); 
         ObjectInputStream in = new ObjectInputStream(fileIn); 

         while(true){ 
          arreglo.add((SuperHeroe) in.readObject()); 

         } 



        } 

         catch(IOException i) 
         { 
         System.out.println("no hay mas elementos\n elementos cargados desde el archivo:"); 

         for(int w=0;w<arreglo.size();w++){ 
          System.out.println(arreglo.get(w).toString()); 
         } 



         }catch(ClassNotFoundException c) 
         { 
         System.out.println("No encontre la clase Estudiante !"); 
         c.printStackTrace(); 

         return; 
         } 
0

您可以按照以下提到的代碼用於處理文件的結尾不同:

public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException { 
    // TODO Auto-generated method stub 

    ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("E://sample.txt")); 
    oos.writeObject(new Integer(5)); 
    oos.writeObject(new Integer(6)); 
    oos.writeObject(new Integer(7)); 
    oos.writeObject(new Integer(8)); 
    oos.flush(); 
    oos.close(); 

    ObjectInputStream ios = new ObjectInputStream(new FileInputStream("E://sample.txt")); 
    Integer temp; 
    try { 
     while ((temp = (Integer) ios.readObject()) != null) { 
      System.out.println(temp); 
     } 
    } catch (EOFException e) { 

    } finally { 
     ios.close(); 
    } 

} 

它會寫,沒有拋出任何從文件中讀取多個整數對象例外。