2012-06-26 29 views
0

我目前正在寫的方法:從內存寫於Android內存/讀任何對象/

  • 讀取對象
  • 寫入對象存儲

我想救一個字符串對象,並再次閱讀,但我的代碼不起作用。

這是我的代碼:

public void writeObjectToMemory(String filename, Object object) { 
     FileOutputStream fos; 
     try { 
      fos = game.openFileOutput(filename, Context.MODE_PRIVATE); 
      ObjectOutputStream os = new ObjectOutputStream(fos); 
      os.writeObject(object); 
      os.close(); 
     } 
     catch (FileNotFoundException e) { 
      e.printStackTrace(); 

     } 
     catch (IOException e) { 
      e.printStackTrace(); 

     } 

    } 

    public void readObjectFromMemory(String filename, Object object) { 
     FileInputStream fis; 
     try { 
      fis = game.openFileInput(filename); 
      ObjectInputStream is = new ObjectInputStream(fis); 
      object = is.readObject(); 
      is.close(); 
     } 
     catch (FileNotFoundException e) { 
      e.printStackTrace(); 

     } 
     catch (StreamCorruptedException e) { 
      e.printStackTrace(); 

     } 
     catch (IOException e) { 
      e.printStackTrace(); 

     } 
     catch (ClassNotFoundException e) { 
      e.printStackTrace(); 

     } 

    } 
+0

問題是什麼,哪部分沒有工作?任何堆棧跟蹤? – Caner

回答

0

您寫道:

public void readObjectFromMemory(String filename, Object object) { 
    //... 
    object = is.readObject(); 

object只有你傳遞給函數的參數的副本。您可以在方法內更改該副本(與您一樣),但這對實際參數沒有影響。

你必須返回你的對象,而不是:

public Object readObjectFromMemory(String filename) { 
    FileInputStream fis; 
    Object obj; 
    try { 
     fis = game.openFileInput(filename); 
     ObjectInputStream is = new ObjectInputStream(fis); 
     obj = is.readObject(); 
     is.close(); 
    } 
    catch (...) { 
     return null; 
    } 

    return obj; 
} 

閱讀此瞭解更多詳情:http://www.cs.utoronto.ca/~dianeh/tutorials/params/

+0

謝謝,我通過這樣做了。 :) –

+0

出於興趣,我將如何更改原始值的值而不返回任何內容? –

+0

唯一的方法是手動將新對象的每個字段複製到舊對象(如果稍後添加/刪除字段,這是一個壞主意)。但是你不能改變對方法內原始對象的引用,只能改變它的字段。 – Dalmas

0

這裏的工作代碼,任何人說有興趣:

注:當使用你readObjectFromMemory(String filename, Object object)將不得不投擲返回的對象。

/** 
    * <b><i>public void writeObjectToMemory(String filename, Object object)</i></b> 
    * <br> 
    * Since: API 1 
    * <br> 
    * <br> 
    * Write a object to the phone's internal storage. 
    * 
    * @param filename 
    * The name of the file you wish to write to. 
    * 
    *  
    */ 

    public void writeObjectToMemory(String filename, Object object) { 
     FileOutputStream fos; 
     try { 
      fos = game.openFileOutput(filename, Context.MODE_PRIVATE); 
      ObjectOutputStream os = new ObjectOutputStream(fos); 
      os.writeObject(object); 
      os.close(); 
      this.gameEngineLog.d(classTAG, "Object successfully written: " + filename); 
     } 
     catch (FileNotFoundException e) { 
      e.printStackTrace(); 
      this.gameEngineLog.d(classTAG, "Object couldn't be opened: " + filename); 

     } 
     catch (IOException e) { 
      e.printStackTrace(); 
      this.gameEngineLog.d(classTAG, "Object couldn't be opened: " + filename); 

     } 

    } 

    /** 
    * <b><i>public Object readObjectFromMemory(String filename, Object object)</i></b> 
    * <br> 
    * Since: API 1 
    * <br> 
    * <br> 
    * Read a object from the phone's internal storage. 
    * 
    * @param filename 
    * The name of the file you wish to read from. 
    * 
    *  
    */ 

    public Object readObjectFromMemory(String filename, Object object) { 
     Object defautObject = null; 
     FileInputStream fis; 
     try { 
      fis = game.openFileInput(filename); 
      ObjectInputStream is = new ObjectInputStream(fis); 
      defautObject = is.readObject(); 
      is.close(); 
      this.gameEngineLog.d(classTAG, "Object successfully read: " + filename); 
     } 
     catch (FileNotFoundException e) { 
      e.printStackTrace(); 
      this.gameEngineLog.d(classTAG, "Object couldn't be opened: " + filename); 

     } 
     catch (StreamCorruptedException e) { 
      e.printStackTrace(); 
      this.gameEngineLog.d(classTAG, "Object couldn't be opened: " + filename); 

     } 
     catch (IOException e) { 
      e.printStackTrace(); 
      this.gameEngineLog.d(classTAG, "Object couldn't be opened: " + filename); 

     } 
     catch (ClassNotFoundException e) { 
      e.printStackTrace(); 
      this.gameEngineLog.d(classTAG, "Object couldn't be opened: " + filename); 

     } 

     return defautObject; 

    }