SharedPreferences 存储对象


//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>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值