用序列化(Serializable)保存、读取对象

   实现Serializable借口的对象可以被转换成一系列字节,并可以在以后使用时完全恢复原来的样子。这一过程也可以在网络中进行,这样就可以先在windows机器上创建一个对象,对其序列化,然后通过网络发送给Linux机器,就可以在Linux机器上准确无误地恢复成原来的样子。整个过程不必关心数据在不同机器上如何表示,也不必关心字节的顺序或其他细节。

      序列化的思想就是“冻结”对象,操作对象(写到磁盘,通过网络传输等),然后“解冻”对象,重新获得可用的Java对象。功能的实现要靠ObjectInputStream/ObjectOutputStream类,完全保真原数据,并且开发愿意用Serializable。

     实现了Serializable接口的类为保证serialVersionUID 值跨不同 java 编译器实现的一致性,序列化类必须声明一个明确的 serialVersionUID 值。

     ClassA.java

public class ClassA implements Serializable {

	private static final long serialVersionUID = 6013572251564847381L;
	private String name = "My name is a";
	private ClassB b = null;

	ClassA() {
		b = new ClassB();
	}

	public String show() {

		System.out.println("a.toString <a.name=\"" + this.name
				+ "\" a.b.name=\"" + this.b.getName() + "\">");

		return "a.toString <a.name=" + this.name + " a.b.name="
				+ this.b.getName() + ">";
		// \" 双引号
		// \' 单引号
		// \\ 反斜线
	}

                 .......................
}

    ClassB.java

public class ClassB implements Serializable{

	private static final long serialVersionUID = -4324044767844361076L;

	private String name="My name is b";
	
	ClassB(){}

	public String getName() {
		return name;
	}

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

将对象内容保存到文本中

WriteSeri.java

public class WriteSeri {

	public static void main(String args[]){
		ObjectOutputStream outObj=null;
		try {
			//将对象内容写入到文本中
			FileOutputStream  outStr=new FileOutputStream("obj.txt");
			outObj=new ObjectOutputStream(outStr);
			ClassA a=new ClassA();
			outObj.writeObject(a);
			System.out.println("write obj:"+a.show());
			outObj.flush();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
				try {
				if (outObj != null) {
					outObj.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}

输出结果:

a.toString <a.name="My name is a" a.b.name="My name is b">
write obj:a.toString <a.name=My name is a a.b.name=My name is b>

将文本内容还原给对象

ReadSeri.java

public class ReadSeri {

	public static void main(String args[]) {
		ObjectInputStream inObj = null;
		try {
			FileInputStream inStr = new FileInputStream("obj.txt");
			inObj = new ObjectInputStream(inStr);
			ClassA a = (ClassA) inObj.readObject();
			System.out.println("read object:" + a.show());
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} finally {
			if (inObj != null) {
				try {
					inObj.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
}

输出结果:

a.toString <a.name="My name is a" a.b.name="My name is b">
read object:a.toString <a.name=My name is a a.b.name=My name is b>

 

 

 

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值