Java 序列化

序列化:将对象转换为一串字节流

反序列化:将字节流转为对象


序列化的几种方式:

1、实现Serializable接口

serialVersionUID就是在反序列化中用来确定由哪个类来加载这个对象

public class A implements Serializable {
	private static final long serialVersionUID = 1L;
	private int age;
	private String name;
//	private  int height;// height能被反序列化
	private transient int height;// height不能被反序列化

	public A(int age, String name, int height) {
		super();
		this.age = age;
		this.name = name;
		this.height = height;
	}

	public int getHeight() {
		return height;
	}

	public void setHeight(int height) {
		this.height = height;
	}

	public int getAge() {
		return age;
	}

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

	public String getName() {
		return name;
	}

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

	@Override
	public String toString() {
		return "A [age=" + age + ", name=" + name + ", height=" + height + "]";
	}

}
public class ObjectStreamDemo {
	public static void main(String[] args) {
		writeObj();
		readObj();
	}

	public static void writeObj() {
		A a = new A(11, "lily", 168);

		try {
			ObjectOutputStream oos = new ObjectOutputStream(
					new FileOutputStream(
							"C:\\text.txt"));
			oos.writeObject(a);
			oos.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void readObj() {
		try {
			ObjectInputStream ois = new ObjectInputStream(
					new FileInputStream(
							"C:\\text.txt"));
			Object obj = ois.readObject();
			A a = (A) obj;// 强转
			System.out.println(a.toString());
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
}

由于height不能被反序列化,结果为:

A [age=11, name=lily, height=0]

transient关键字只能与Serializable接口搭配使用

2、实现Externalizable接口

需要实现readExternal和writeExternal两个扩展方法

在writeExternal方法中定义那些能序列化,哪些不能被序列化,在readExternal需按照writeExternal中写入的顺序读取

Serializable中transient和static修饰的字段不能被序列化,而在Externalizable中无效

public class B implements Externalizable {

	private int age;
	private String name;
	private transient int height;
	private static int aa;

	public static int getAa() {
		return aa;
	}

	public static void setAa(int aa) {
		B.aa = aa;
	}

	public B() {
		super();
	}

	public B(int age, String name, int height) {
		super();
		this.age = age;
		this.name = name;
		this.height = height;
		this.aa = 12;
	}

	public int getAge() {
		return age;
	}

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

	public String getName() {
		return name;
	}

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

	public int getHeight() {
		return height;
	}

	public void setHeight(int height) {
		this.height = height;
	}




	@Override
	public String toString() {
		return "B [age=" + age + ", name=" + name + ", height=" + height + ", aa=" + aa + "]";
	}

	@Override
	public void readExternal(ObjectInput in) throws IOException,
			ClassNotFoundException {
		name = (String) in.readObject();
		age = (int) in.readObject();
		height = (int) in.readObject();
		aa = (int) in.readObject();
		System.out.println("B [age=" + age + ", name=" + name + ", height="
				+ height + ", aa=" + aa + "]");

	}

	@Override
	public void writeExternal(ObjectOutput out) throws IOException {
		// 在writeExternal方法中定义那些能序列化,哪些不能被序列化
		out.writeObject(name);// 顺序写
		out.writeObject(age);
		out.writeObject(height);
		out.writeObject(aa);

	}
}
public class Operate {
	// 序列化
	public void serializable(B b) throws FileNotFoundException, IOException {
		ObjectOutputStream oos = new ObjectOutputStream(
				new FileOutputStream(
						"text.txt"));
		oos.writeObject(b);
		oos.close();
	}

	// 反序列化
	public B deserializable() throws FileNotFoundException, IOException,
			ClassNotFoundException {
		ObjectInputStream ois = new ObjectInputStream(
				new FileInputStream(
						"text.txt"));
		B b = (B) ois.readObject();
		ois.close();
		return b;
	}

}

public class MainTest {

	public static void main(String[] args) throws FileNotFoundException,
			IOException, ClassNotFoundException {
		Operate operate = new Operate();
		B b = new B(11, "lily", 168);
		operate.serializable(b);// 序列化
		B b1 = operate.deserializable();// 反序列化

		System.out.println(b1.toString());
	}

}

结果为:
B [age=11, name=lily, height=168, aa=12]

其中被transient修饰的height和static修饰的aa均能被序列化

3、将对象转换为json字符串


若您还知道别的更好的方法,希望您不吝赐教,谢谢~



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值