gpt4 book ai didi

android - 将对象序列化为android中的文件

转载 作者:行者123 更新时间:2023-11-29 21:55:11 25 4
gpt4 key购买 nike

现在写下我希望能够使用序列化保存和打开的此类:

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;
}
}
}

以下是我的“屏幕”类,它充当我上面的模型和我的 View (屏幕)的 Controller :

// ----------------------------------------------------------
/**
* 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 中的文件,我很乐意听到它。谢谢!

最佳答案

基本上就是这样。但是你不必像你那样处理字节。我使用这两种方法将 Serializable 对象写入和读取到 Android 应用程序中的私有(private)文件。

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;
}

关于android - 将对象序列化为android中的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13392571/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com