2012-11-15 35 views
1

寫信跟我有這個類,我想能夠保存和打開使用序列:序列化對象到文件中的android

public class Region 
implements Serializable 
{ 
private final int  inputNumberOfColumnsAlongXAxis; 
private final int  inputNumberOfColumnsAlongYAxis; 
private double   inputDataScaleReductionOnXAxis; 
private double   inputDataScaleReductionOnYAxis; 

private int   numberOfColumnsAlongXAxis; 
private int   numberOfColumnsAlongYAxis; 
private int   cellsPerColumn;    // Z-Axis dimension 
private float   inhibitionRadius; 
private final float percentMinimumOverlapScore; 
private final float minimumOverlapScore; 

// ---------------------------------------------------------- 
/** 
* Save the current Region object on the view into a file named 
* "TrainedRegion". 
*/ 
public static byte[] serializeObject(Object region) 
{ 
    // TODO: save with current timestamp and use open pop-up 
    // String timeStamp = "" + System.currentTimeMillis()/1000; 
    // String fileName = ("Region created at timestamp(seconds): " + 
    // timeStamp); 
    ByteArrayOutputStream baos = new ByteArrayOutputStream(); 

    try { 
     ObjectOutput objectOutput = new ObjectOutputStream(baos); 
     objectOutput.writeObject(region); 
     objectOutput.close(); 

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

     return bytesOfSerializedObject; 
    } catch(IOException ioe) { 
     Log.e("serializeObject", "error", ioe); 

     return null; 
    } 
} 


// ---------------------------------------------------------- 
/** 
* Returns a previously saved Region object with the given name. 
* 
* @param fileName 
* @return A previously saved Region object. 
*/ 
public static Object deserializeObject(byte[] bytes) 
{ 
    // TODO: later read Region object saved in file named by the time stamp during 
    // saving. 
    // ObjectInputStream inputStream = new ObjectInputStream(new 
    // FileInputStream(fileName)); 

    try { 
     ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bytes)); 
     Object object = in.readObject(); 
     in.close(); 

     return object; 
     } catch(ClassNotFoundException cnfe) { 
     Log.e("deserializeObject", "class not found error", cnfe); 

     return null; 
     } catch(IOException ioe) { 
     Log.e("deserializeObject", "io error", ioe); 

     return null; 
     } 
} 
} 

以下是在我的「屏幕」類,它充當控制器我的模型上面和我的視圖(畫面):

// ---------------------------------------------------------- 
/** 
* I want to be able to open a Region instance that was saved. 
*/ 
public void openClicked() 
{ 
    // open the file created below in save method and write all the bytes into 
    // the global region instance for this class. How can do this? 

    this.region = (Region) this.region.deserializeObject(savedRegionInBytes); 
} 


// ---------------------------------------------------------- 
/** 
* 
*/ 
public void saveClicked() 
{ 
    this.savedRegionInBytes = this.region.serializeObject(this.region); 

    // TODO: write savedRegionInBytes to a file. How can I do this? 
} 

而且如果有對象序列化爲android系統中的文件,我很想聽到一個更簡單的方法。謝謝!

回答

2

基本上就是這樣做的方法。但是你不必像你那樣處理字節。 我使用這兩種方法在Android應用程序中編寫和讀取Serializable對象到私有文件。

public static boolean saveRegion(Context context, Region region) { 
    try { 
     FileOutputStream fos = context.openFileOutput(REGION_FILENAME, Context.MODE_PRIVATE); 
     ObjectOutputStream oos = new ObjectOutputStream(fos); 
     oos.writeObject(region); 
     oos.close(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
     return false; 
    } 

    return true; 
} 

public static Region getRegion(Context context) { 
    try { 
     FileInputStream fis = context.openFileInput(REGION_FILENAME); 
     ObjectInputStream is = new ObjectInputStream(fis); 
     Object readObject = is.readObject(); 
     is.close(); 

     if(readObject != null && readObject instanceof Region) { 
      return (Region) readObject; 
     } 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } catch (ClassNotFoundException e) { 
     e.printStackTrace(); 
    } 

    return null; 
} 
+0

非常感謝您的幫助!它很棒!另一個問題,如果我添加saveRegion(...,...,String fileName)的另一個參數和getRegion(...,String fileName)相同的參數,我將能夠獲得與該文件名一起保存的區域對象? –

+0

是的,你可以。 REGION_FILENAME只是一個字符串。您可以在描述中向該方法添加一個參數,從而可以寫入不同的文件。 – stromvap