Serialisieren von Objekten in Dateien in android
Schreiben, jetzt habe ich diese Klasse, die ich möchte in der Lage sein, zu speichern und zu öffnen durch Serialisierung:
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;
}
}
}
Folgendes ist in meinem "Bildschirm" - Klasse, die fungiert als controller für mein Modell oben, und meine Ansicht(auf dem Bildschirm):
//----------------------------------------------------------
/**
* 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?
}
Auch wenn es ein einfacher Weg zum serialisieren von Objekten in Dateien in android würde ich es gerne hören. Danke!
Du musst angemeldet sein, um einen Kommentar abzugeben.
Das ist im Grunde die Weise, es zu tun. Aber Sie müssen nicht behandeln bytes, wie Sie es tun.
Ich benutze diese zwei Methoden zum schreiben und Lesen ein Serialisiertes Objekt auf eine private Datei in eine Android-app.