您的位置:首页 > 其它

王学岗对象的序列化与反序列化

2017-01-02 16:07 148 查看
序列化:将一个对象转化为字节序列的过程;

反序列化:将字节序列转化为对象的过程

注意:序列化和反序列化操作过程中要保证是同一个对象;

import java.io.Serializable;

public class PersonBean implements Serializable {
public PersonBean() {
super();
// TODO Auto-generated constructor stub
}

public PersonBean(String name, int age, String sex) {
super();
this.name = name;
this.age = age;
this.sex = sex;
}

private String name;
private int age;
private String sex;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

public String getSex() {
return sex;
}

public void setSex(String sex) {
this.sex = sex;
}
}


import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class TestMain {

/**
* @param args
* @throws IOException
* @throws FileNotFoundException
* @throws ClassNotFoundException
*/
public static void main(String[] args) throws FileNotFoundException,
IOException, ClassNotFoundException {
// TODO Auto-generated method stub
serializableBean(new PersonBean("张欣", 18, "男"));
PersonBean personBean = deserializableBean();
System.out.println(personBean.getName());
}

// 序列化对象
public static void serializableBean(PersonBean personBean)
throws FileNotFoundException, IOException {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(
"D://a.text"));
oos.writeObject(personBean);
}

// 反序列化对象
public static PersonBean deserializableBean() throws FileNotFoundException,
IOException, ClassNotFoundException {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(
"D://a.text"));
Object object = ois.readObject();
return (PersonBean) object;
}
}


我们现在在android实现

package com.example.android;

import java.io.FileNotFoundException;
import java.io.IOException;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends Activity {

private PersonBean personBean;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
personBean = new PersonBean("张欣", 18, "男");
}
//单击事件
public void click(View view) throws FileNotFoundException, IOException {
//联想我们把数据放入bundle中
SerializableUtils.serializableBean(personBean);
startActivity(new Intent(this,TwoActivity.class));
}
}


package com.example.android;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

import android.os.Environment;

public class SerializableUtils {
// 得到sdcard目录
public static File getFileSdcard() {
File file = new File(Environment.getExternalStorageDirectory(),
"serializable");
if (!file.exists()) {
file.mkdirs();
}
return file;
}

// 序列化对象
public static void serializableBean(Serializable serializable)
throws FileNotFoundException, IOException {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(
getFileSdcard().getAbsoluteFile()+File.separator+"a.text"));
oos.writeObject(serializable);
}

// 反序列化对象
public static <T extends Serializable>T deserializableBean(Class<T> clazz)
throws FileNotFoundException, IOException, ClassNotFoundException {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(
getFileSdcard().getAbsoluteFile()+File.separator+"a.text"));
Object object = ois.readObject();
return (T) object;
}
}


package com.example.android;
import java.io.Serializable; public class PersonBean implements Serializable { public PersonBean() { super(); // TODO Auto-generated constructor stub } public PersonBean(String name, int age, String sex) { super(); this.name = name; this.age = age; this.sex = sex; } private String name; private int age; private String sex; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } }


package com.example.android;

import java.io.FileNotFoundException;
import java.io.IOException;

import android.app.Activity;
import android.os.Bundle;

public class TwoActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.two);
try {
PersonBean personBean = SerializableUtils
.deserializableBean(PersonBean.class);
System.out.println(personBean.getName());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: