//Person类
package com.example.saveobject;
import java.io.Serializable;
public class Person implements Serializable {
private static final long serialVersionUID = 1L;
String name;
int age;
String address;
String education;
String tel;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getEducation() {
return education;
}
public void setEducation(String education) {
this.education = education;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
String sex;
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + ", address=" + address
+ ", education=" + education + ", tel=" + tel + ", sex=" + sex
+ "]";
}
}
//MainActivity
package com.example.saveobject;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import android.support.v7.app.ActionBarActivity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity implements OnClickListener{
Person person = null;
long startTime = 0l;
long endTime = 0l;
private Button bu1,bu2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bu1=(Button) findViewById(R.id.button1);
bu2=(Button) findViewById(R.id.button2);
bu1.setOnClickListener(this);
bu2.setOnClickListener(this);
person = new Person();
person.setName("LULU");
person.setSex("bitch");
person.setAddress("北京海淀");
person.setAge(20);
person.setTel("1231312");
person.setEducation("小学");
}
/**
* 序列化对象
*/
private String serialize(Person person) throws IOException {
startTime = System.currentTimeMillis();
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ObjectOutputStream objectOutputStream = new ObjectOutputStream(
byteArrayOutputStream);
objectOutputStream.writeObject(person);
String serStr = byteArrayOutputStream.toString("ISO-8859-1");
serStr = java.net.URLEncoder.encode(serStr, "UTF-8");
objectOutputStream.close();
byteArrayOutputStream.close();
Log.d("serial", "serialize str =" + serStr);
endTime = System.currentTimeMillis();
Log.d("serial", "序列化耗时为:" + (endTime - startTime));
return serStr;
}
/**
* 反序列化对象
*
*/
private Person deSerialization(String str) throws IOException,
ClassNotFoundException {
startTime = System.currentTimeMillis();
String redStr = java.net.URLDecoder.decode(str, "UTF-8");
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(
redStr.getBytes("ISO-8859-1"));
ObjectInputStream objectInputStream = new ObjectInputStream(
byteArrayInputStream);
Person person = (Person) objectInputStream.readObject();
objectInputStream.close();
byteArrayInputStream.close();
endTime = System.currentTimeMillis();
Log.d("serial", "反序列化耗时为:" + (endTime - startTime));
return person;
}
void saveObject(String strObject) {
SharedPreferences sp = getSharedPreferences("person", 0);
Editor edit = sp.edit();
edit.putString("person", strObject);
edit.commit();
}
String getObject() {
SharedPreferences sp = getSharedPreferences("person", 0);
return sp.getString("person", "");
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1:
try {
saveObject(serialize(person));
} catch (IOException e) {
e.printStackTrace();
}
break;
case R.id.button2:
try {
Person person = deSerialization(getObject());
Log.d("serial", person.toString());
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
break;
default:
break;
}
}
}
//activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.saveobject.MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="保存对象" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/button1"
android:layout_centerHorizontal="true"
android:text="解读对象" />
</RelativeLayout>