java字符串反序列化_java序列化对象为字符串(及反序列化)

Person.java package com.example.shareddatatest; 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.java package com.example.shareddatatest; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.util.HashMap; import java.util.List; import android.app.Activity; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.os.Bundle; import android.util.Log; import android.view.Menu; import android.view.View; public class MainActivity extends Activity { Person person = null; long startTime = 0l; long endTime = 0l; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); person = new Person(); person.setName("LULU"); person.setSex("bitch"); person.setAddress("北京海淀"); person.setAge(20); person.setTel("1231312"); person.setEducation("小学"); } /** * 序列化对象 *  * @param person * @return * @throws IOException */ 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; } /** * 反序列化对象 *  * @param str * @return * @throws IOException * @throws ClassNotFoundException */ 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", null); } public void onclick(View v) { switch (v.getId()) { case R.id.button1: try { saveObject(serialize(person)); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } break; case R.id.button2: try { Person person = deSerialization(getObject()); Log.d("serial", person.toString()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } break; default: break; } } }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值