2013-03-13 88 views
0

我有一個數據存儲類,試圖從iOS項目中進行調整。它適用於iOS,它幾乎適用於我的android項目,但是,當從任務管理器退出android應用程序時,該文件在下次運行時不會恢復。Android應用程序在完全退出時刪除文件(退出任務管理器)

該類的意圖是,如果在保存對象時沒有文件,類將創建該文件並保存它。如果有文件,該類將打開包含數組的文件,將該對象追加到該數組,並保存該文件。

有沒有什麼,我失蹤持久文件到設備上?

這是我如何調用類的文件

ObjectStore.defaultStore().saveObject(getApplicationContext(), anObject); 

,這裏是我的ObjectStore類

public class ObjectStore { 

ArrayList<Object> objectList = new ArrayList<Object>(); 
static ObjectStore defaultStore = null; 
Context context = null; 

public static ObjectStore defaultStore(){ 
    if(defaultStore == null){ 
     defaultStore = new ObjectStore(); 
    } 
    return defaultStore; 
} 

public Object ObjectStore(){ 
    if(defaultStore != null){ 
     return defaultStore; 
    } 
    return this; 
} 

public ArrayList<Object> objectList(){ 
    return objectList; 
} 

public Object saveObject(Context c, Object object){ 
    context = c; 
    objectList.add(object); 
    saveFile(context); 
    return object; 
} 

public void clearAll(){ 
    objectList.clear(); 
    saveFile(context); 
} 

public boolean doesFileExist(Context c){ 
    context = c; 
    File file = c.getFileStreamPath("objectList.file"); 
    if(file.exists()){ 
     return true; 
    }else{  
     return createFile(context); 
    } 
} 

public boolean createFile(Context c){ 
    context = c; 
    try{ 
     FileOutputStream fos = context.openFileOutput("objectList"+".file", Context.MODE_PRIVATE); 
     ObjectOutputStream oos = new ObjectOutputStream(fos); 
     oos.writeObject(objectList); 
     oos.close(); 
     return true; 
    }catch(IOException e){ 
     e.printStackTrace(); 
     Log.d("TAG", "Error creating file: " + e); 
     return false; 
    } 
} 

public boolean saveFile(Context c){ 
    Log.d("TAG", "Trying to save file"); 
    context = c; 
    try{ 
     FileOutputStream fos = context.openFileOutput("objectList.file", Context.MODE_PRIVATE); 
     ObjectOutputStream oos = new ObjectOutputStream(fos); 
     oos.writeObject(objectList); 
     oos.close(); 
     Log.d("TAG", "File Saved"); 
     return true; 
    }catch(IOException e){ 
     e.printStackTrace(); 
     Log.d("TAG", "Error saving file: " + e); 
     return false; 
    } 
} 

@SuppressWarnings("unchecked") 
public ArrayList<Object> loadFile(Context c) throws Exception{ 
    Log.d("TAG", "Trying to load file"); 
    context = c; 
    if(doesFileExist(context)){ 
     try{ 
      FileInputStream fis = context.openFileInput("objectList.file"); 
      ObjectInputStream in = new ObjectInputStream(fis); 
      objectList = (ArrayList<Object>) in.readObject(); 
      in.close(); 
      Log.d("TAG", "File Loaded"); 
      return objectList; 
     }catch(IOException e){ 
      e.printStackTrace(); 
      Log.d("TAG", "Error loading file: " + e); 
      return null; 
     } 
    } 
    return null; 
} 
} 
+0

寫入文件時,您將其保存爲objectList.file ...打開文件時,您正在打開objectList.data。希望有所幫助。 – Jeremy 2013-03-13 03:48:07

+0

對不起,這張照片發佈時很失誤,不過很好看。我只希望這是問題:-) – 2013-03-13 03:50:27

+0

你有沒有通過'doesFileExist(context)'檢查? – Jeremy 2013-03-13 03:54:45

回答

1

你的loadFile()永遠不會被調用保存!這裏沒有任何東西叫它! ;)