2011-04-28 21 views

回答

42

總之你不能,試試你的序列化對象的私有文件,它相當於同樣的事情。下面的示例類:

import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.ObjectInputStream; 
import java.io.ObjectOutputStream; 

import android.app.Activity; 
import android.content.Context; 

/** 
* 
* Writes/reads an object to/from a private local file 
* 
* 
*/ 
public class LocalPersistence { 


    /** 
    * 
    * @param context 
    * @param object 
    * @param filename 
    */ 
    public static void witeObjectToFile(Context context, Object object, String filename) { 

     ObjectOutputStream objectOut = null; 
     try { 

      FileOutputStream fileOut = context.openFileOutput(filename, Activity.MODE_PRIVATE); 
      objectOut = new ObjectOutputStream(fileOut); 
      objectOut.writeObject(object); 
      fileOut.getFD().sync(); 

     } catch (IOException e) { 
      e.printStackTrace(); 
     } finally { 
      if (objectOut != null) { 
       try { 
        objectOut.close(); 
       } catch (IOException e) { 
        // do nowt 
       } 
      } 
     } 
    } 


    /** 
    * 
    * @param context 
    * @param filename 
    * @return 
    */ 
    public static Object readObjectFromFile(Context context, String filename) { 

     ObjectInputStream objectIn = null; 
     Object object = null; 
     try { 

      FileInputStream fileIn = context.getApplicationContext().openFileInput(filename); 
      objectIn = new ObjectInputStream(fileIn); 
      object = objectIn.readObject(); 

     } catch (FileNotFoundException e) { 
      // Do nothing 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } catch (ClassNotFoundException e) { 
      e.printStackTrace(); 
     } finally { 
      if (objectIn != null) { 
       try { 
        objectIn.close(); 
       } catch (IOException e) { 
        // do nowt 
       } 
      } 
     } 

     return object; 
    } 

} 
+0

應該'filename'在你的代碼是絕對路徑(如:「/data/com.example.myapp/foobar.dat」),或者它應該是相對的(例如:「foobar.dat」)? – 2011-08-30 22:21:27

+3

只是一個文件名,路徑分隔符是不允許的,因爲在API文檔http://developer.android.com/reference/android/content/Context.html#openFileOutput(java.lang.String,詮釋說明) – 2011-08-31 14:18:22

+1

如何我刪除上面的方法保存的文件? – 2014-04-09 13:41:35

18

如果對象是簡單的POJO,你可以對象轉換爲JSON字符串,並將其與putString保存在共享偏好()。

12

這是可能做到這一點沒有文件。

我序列化信息的base64像這樣我可以把它保存爲喜好的字符串。

以下代碼將序列化對象序列化爲base64字符串,反之亦然: import android.util.Base64;

import java.io.ByteArrayInputStream; 
import java.io.ByteArrayOutputStream; 
import java.io.IOException; 
import java.io.ObjectInputStream; 
import java.io.ObjectOutputStream; 
import java.io.Serializable; 


public class ObjectSerializerHelper { 
    static public String objectToString(Serializable object) { 
     String encoded = null; 
     try { 
      ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); 
      ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream); 
      objectOutputStream.writeObject(object); 
      objectOutputStream.close(); 
      encoded = new String(Base64.encodeToString(byteArrayOutputStream.toByteArray(),0)); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     return encoded; 
    } 

    @SuppressWarnings("unchecked") 
    static public Serializable stringToObject(String string){ 
     byte[] bytes = Base64.decode(string,0); 
     Serializable object = null; 
     try { 
      ObjectInputStream objectInputStream = new ObjectInputStream(new ByteArrayInputStream(bytes)); 
      object = (Serializable)objectInputStream.readObject(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } catch (ClassNotFoundException e) { 
      e.printStackTrace(); 
     } catch (ClassCastException e) { 
      e.printStackTrace(); 
     } 
     return object; 
    } 

} 
14

接受的答案是誤導,我們可以使用GSON將可序列化對象存儲到SharedPreferences中。在google-gson閱讀更多關於它的信息。

您可以添加GSON依賴於搖籃與文件:

compile 'com.google.code.gson:gson:2.7' 

這裏的片段:

首先,創建你平時sharedPreferences:

//Creating a shared preference 
SharedPreferences mPrefs = getPreferences(MODE_PRIVATE); 

保存從序列化對象的偏好:

Editor prefsEditor = mPrefs.edit(); 
Gson gson = new Gson(); 
String json = gson.toJson(YourSerializableObject); 
prefsEditor.putString("SerializableObject", json); 
prefsEditor.commit(); 

獲取序列化對象的偏好:

Gson gson = new Gson(); 
String json = mPrefs.getString("SerializableObject", ""); 
yourSerializableObject = gson.fromJson(json, YourSerializableObject.class);