Android serialization serialization

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.