Android serialization
Objective:
to facilitate the testing of sensor data processing algorithms, the acquisition data of the Android program is saved as a file, and the PC program tests the algorithm on the PC by deserializing the object .
JavaSerialization:
will beJavaobject is serialized as a binary file.JavaSerialization technology isJavaone of the more important technical points in a series of technologies, in most cases, developers only need to understand that the class being serialized needs to be implementedSerializableinterface, usingObjectInputStreamand theObjectOutputStreamto read and write objects.
Precautions :
The class of the object to serialize is to meet the following conditions:
1. class to implement Serializable interface
2. Serialize and deserialize procedures to ensure package consistency
3.serialVersionUID to be consistent
Routines :
Package Com.example.coolwaterld.serializedemo;import Java.io.serializable;class Person implements Serializable// Added implements serializable{ String name= ""; Private String number= ""; Private String address= ""; Private static final long serialversionuid = 1L; Public String getName () { return name; } Public String GetNumber () { return number; } Public String getaddress () { return address; }}
Package Com.example.coolwaterld.serializedemo;import Android.app.activity;import Android.os.bundle;import Android.util.log;import Java.io.file;import Java.io.fileinputstream;import Java.io.fileoutputstream;import Java.io.objectinputstream;import Java.io.objectoutputstream;public class Mainactivity extends Activity {@Override p ublic void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); person person = new person (); Person.setname ("Coolwaterld"); Person.setaddress ("BJ"); Person.setnumber ("123"); Save the object Saveobject (person); Get the Object person person1 = (person) loadserializedobject (new File ("/sdcard/person.bin")); LOG.I ("Name", Person1.getname ()); } public void Saveobject (person p) {try {objectoutputstream oos = new ObjectOutputStream (new FileOutputStream (New File ("/sdcard/kvmatrix.bin")); Oos.writeobject (P); Oos.flush (); Oos.close (); } catch (Exception ex) {log.v ("Serialize write:", Ex.getmessage ()); Ex.printstacktrace (); }} public Object Loadserializedobject (File f) {try {objectinputstream ois = new Objec Tinputstream (new FileInputStream (f)); Object o = Ois.readobject (); return o; } catch (Exception ex) {log.v ("Serialize Read:", Ex.getmessage ()); Ex.printstacktrace (); } return null; }}
<uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/> in Androidmanifest.xml
Reference:
Java–serialization
Http://www.tutorialspoint.com/java/java_serialization.htm
Advanced understanding of Java serialization
http://www.ibm.com/developerworks/cn/java/j-lo-serial/index.html?ca=drs-#icomments
( serialization ID problem , static variable serialization , parent class serialization with Transient keyword , encryption of sensitive fields, Serialization of storage rules )
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Android serialization serialization