2013-01-13 49 views
2

當我的應用程序啓動時,它連接到一個數據庫並下載一些XML格式的數據。這是佈局:寫一個ArrayList到SD卡

<Customers> 
    <Customer> 
    <name>...</name> 
    ... 
    </Customer> 
    <Customer> 
    <name>...</name> 
    ... 
    </Customer> 
    ... 
</Customer> 

對於我創建類Customer wicht我存儲在一個ArrayList中的對象的每個客戶。用戶可以編輯和添加每個客戶的一些數據,並可以將其上傳回服務器。

問題是我不知道在本地存儲數據的最佳方式是什麼,所以如果我的應用程序關閉或用戶沒有互聯網了,他們會有備份。我現在將所有Customer對象轉換回XML,然後將其存儲在SD卡上,但這不是我認爲的好解決方案。每個客戶都有大約40個字符串,10個整數和一些我必須存儲的布爾值。有沒有更好的方式在本地存儲數據?

我發現some code這個可以存儲一個ArrayList,但它不適用於自定義類。

編輯:我用this tutorial來解決我的問題。

import java.io.BufferedInputStream; 
import java.io.BufferedOutputStream; 
import java.io.ByteArrayInputStream; 
import java.io.ByteArrayOutputStream; 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.ObjectInputStream; 
import java.io.ObjectOutput; 
import java.io.ObjectOutputStream; 
import java.util.ArrayList; 

import android.os.Environment; 

public class SerializeData { 
    public static byte[] serializeObject(Object o) { 
     ByteArrayOutputStream bos = new ByteArrayOutputStream(); 

     try { 
      ObjectOutput out = new ObjectOutputStream(bos); 
      out.writeObject(o); 
      out.close(); 

      // Get the bytes of the serialized object 
      byte[] buf = bos.toByteArray(); 

      return buf; 
     } catch (IOException ioe) { 
      CreateLog.addToLog(ioe.toString()); 
      return null; 
     } 
    } 

    public static Object deserializeObject(byte[] b) { 
     try { 
      ObjectInputStream in = new ObjectInputStream(
        new ByteArrayInputStream(b)); 
      Object object = in.readObject(); 
      in.close(); 

      return object; 
     } catch (ClassNotFoundException cnfe) { 
      CreateLog.addToLog(cnfe.toString()); 

      return null; 
     } catch (IOException ioe) { 
      CreateLog.addToLog(ioe.toString()); 

      return null; 
     } 
    } 

    public static void saveData(){ 
     byte[] arrayData = SerializeData 
       .serializeObject(MyTasks.allCustomers); 

     BufferedOutputStream bos; 
     try { 
      bos = new BufferedOutputStream(new FileOutputStream(
        Environment.getExternalStorageDirectory() 
          + "/myprogram/myarray.txt")); 
      bos.write(arrayData); 
      bos.flush(); 
      bos.close(); 
     } catch (FileNotFoundException e) { 
      CreateLog.addToLog(e.toString()); 
     } catch (IOException e) { 
      CreateLog.addToLog(e.toString()); 
     } 
    } 

    public static ArrayList<Customer> getData(){ 
     File afile = new File(Environment.getExternalStorageDirectory() 
       + "/myprogram/myarray.txt"); 
     int size = (int) afile.length(); 
     byte[] bytes = new byte[size]; 
     try { 
      BufferedInputStream buf = new BufferedInputStream(new FileInputStream(afile)); 
      buf.read(bytes, 0, bytes.length); 
      buf.close(); 
     } catch (FileNotFoundException e) { 
      CreateLog.addToLog(e.toString()); 
     } catch (IOException e) { 
      CreateLog.addToLog(e.toString()); 
     } 

     return (ArrayList<Customer>) SerializeData.deserializeObject(bytes); 

    } 
} 
+0

」有沒有更好的方式在本地存儲數據?「 - SQLite數據庫將是我的第一選擇。 – Squonk

回答

1

據我所知,客戶結構只能在本地讀取和寫入,也可以通過相同或高度相關的程序進行讀寫。對於這種情況,我認爲Java序列化是合適的,因爲它只增加很少的代碼。

讓客戶實現Serializable(立即放入serialVersionUID,以便以後可以控制版本)。 ArrayList已經是可序列化的。獲取文件名,如here所述,並使用ObjectInput/Output流進行序列化。如果需要,您可以隨後遷移到XML/JSON。一個簡單的「hello world」序列化示例可以在here找到。 「

+0

非常感謝,我用這個代碼:http://www.jondev.net/articles/Android_Serialization_Example_(Java) – ObAt