2016-11-22 33 views
0

我基本上做一個日記應用程序,其中每個單獨的日記條目需要堅持,我想保留所有條目在一個單一的文件。序列化多個條目到同一個文件

我見過噸教程上序列化一個對象,所以我想出了這個解決方案,(不工作)但即使我設法解決它,感覺就像一個草率的解決方案。

(在這裏,我試圖序列一個ArrayList,每一次我保存的條目時,我反序列化列表,再次序列化之前添加新條目列表)

爲了澄清,我的問題是:這是將對象保存到同一個文件的好方法嗎?

還是沒有人有一些關於別的東西的提示我應該嘗試一下,關於這個視頻或文檔的鏈接也很感激。

public class Serializer 
{ 

    //Calls readFile and adds the returned entries to an ArrayList 
    //Add the target object to the list and write to the file 
    public static void writeToFile(Object target) 
    { 
     ArrayList entries = new ArrayList(); 

     entries = readFile(); 
     entries.add(target); 


     String filename = "entries.bin"; 

     FileOutputStream fileOut = null; 
     ObjectOutputStream objOut = null; 

     try 
     { 
      fileOut = new FileOutputStream(filename); 
      objOut = new ObjectOutputStream(fileOut); 
      objOut.writeObject(entries); 
      objOut.close(); 
     } 
     catch(IOException ex) 
     { 
      ex.printStackTrace(); 
     } 
    } 

    //Reads the file and returns all entries in a list 
    public static ArrayList readFile() 
    { 
     ArrayList persistedEntries = new ArrayList<>(); 
     String filename = "entries.bin"; 

     FileInputStream fileIn = null; 
     ObjectInputStream objIn= null; 
     try 
     { 
      fileIn = new FileInputStream(filename); 
      objIn = new ObjectInputStream(fileIn); 
      persistedEntries = (ArrayList) objIn.readObject(); 
      objIn.close(); 
     } 
     catch(IOException ex) 
     { 
      ex.printStackTrace(); 
     } 
     catch(ClassNotFoundException ex) 
     { 
      ex.printStackTrace(); 
     } 

     return persistedEntries; 
    } 
} 
+0

這可能幫助,如果你還別說在這種方式,它「不工作」 - 它拋出一個異常?哪一個? – Hulk

+0

我實際上並不確定什麼是不工作的,因爲我得到一個索引超出範圍的異常,如果我已經理解不應該能夠發生的ArrayLists。但我只試圖運行程序,而不是自己的方法。 但是,正如你所看到的,我的問題並不是關於代碼不工作的事實,我只是想看看這個「想法」是否好,或者如果我完全錯過了某些東西,方向。 :) –

+0

看着你的代碼,我注意到你在編寫一個文件之前總是「讀取」。必須檢查空白文件'entries.bin'以查看返回的ArrayList對象是什麼? – jrbeverly

回答

0

這是對象保存到同一文件的好方法,在多個場合?

我會認爲沒有。這是因爲您的方法writeToFile或更準確地說appendToFile可能會在邊緣情況下引入奇怪的行爲(例如具有意外對象的entries.bin)。我會爲此辯解:

使用writeToFile(ArrayList<Object> target)覆蓋指定數組的文件。然後添加一個方法appendToFile(Object target),該方法處理從磁盤讀取entries.bin的過程,附加target然後將該數組寫入磁盤。這具有分離與將新對象target與磁盤上的文件「合併」相關的任何邏輯以及寫入entries.bin文件的實際邏輯的優點。

如果只是一個學習練習,我會去與上述。潛在resource


添加新格式:

public class Serializer 
{ 
    private String filename; 

    // pass in "entries.bin" 
    public Serializer(String filename) { 
     this.filename = filename; 
    } 

    public void append(Object target) { 
     // readfile will return at least empty arraylist 
     ArrayList entries = readFile(); 
     entries.add(target); 
     serialize(entries); 
    } 

    public void serialize(ArrayList entries) 
    { 
     FileOutputStream fileOut = null; 
     ObjectOutputStream objOut = null; 
     try 
     { 
      fileOut = new FileOutputStream(filename); 
      objOut = new ObjectOutputStream(fileOut); 
      objOut.writeObject(entries); 
      objOut.close(); 
     } 
     catch(IOException ex) 
     { 
      ex.printStackTrace(); 
     } 
    } 

    //Reads the file and returns all entries in a list 
    public ArrayList deserialize() 
    { 
     ArrayList persistedEntries = new ArrayList<>(); 

     FileInputStream fileIn = null; 
     ObjectInputStream objIn = null; 
     try 
     { 
      fileIn = new FileInputStream(filename); 
      objIn = new ObjectInputStream(fileIn); 

      Object result = objIn.readObject(); 
      if (!(result instanceof ArrayList)) { 
       // read object is not an arraylist 
      } 

      persistedEntries = (ArrayList) objIn.readObject(); 
      objIn.close(); 
     } 
     catch(IOException ex) 
     { 
      ex.printStackTrace(); 
     } 
     catch(ClassNotFoundException ex) 
     { 
      ex.printStackTrace(); 
     } 

     return persistedEntries; 
    } 
} 
+0

爲什麼?第二種方法完全等同於添加到列表中,然後調用第一種方法。兩行代碼。 – EJP

+0

第二種方法你的意思是'appendToFile'?正如我上面所說,它是關於分離邏輯和良好的編碼實踐。通過代碼他正在使用'writeToFile'來處理以下任務:構造內存數據,從源數據讀取數據,將源數據與內存數據同步,將數據寫入源代碼。以及與此相關的所有潛在錯誤和邊緣情況。他將每個責任分解成一個單獨的方法更有意義。 – jrbeverly

相關問題