java序列化存储_Java对象序列化存储实现

package com.inky.thinking.chapter1;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.ObjectInput;

import java.io.ObjectInputStream;

import java.io.ObjectOutput;

import java.io.ObjectOutputStream;

import java.util.ArrayList;

import java.util.Collection;

import junit.framework.TestCase;

/**

* 该类实现接口Serializable 因此可以序列化存储到硬盘

* 如果没有实现该接口则在从文件读取时会通过构造器构造而不是读取

* @author inky

*两个属性 name addr 其中addr被transient修饰

*/

class MyClass implements java.io.Serializable{

public String name;

/**

* transient修饰

*/

transient public String addr;

public MyClass(int x){

name = "this is name for "+x;

addr = "this is addr for "+x;

System.out.println("构造函数被调用");

}

public String toString(){

return "name:\t"+name+"\naddr:\t"+addr;

}

}

/**

* 在上一个基础上实现了java.io.Externalizable接口

* 该接口有两个方法readExternal writeExternal

* @author inky

*

*/

class MyClass2 implements java.io.Serializable,java.io.Externalizable{

public String name;

/**

* 这里没有修饰

*/

public String addr;

public MyClass2(){

System.out.println("无参构造函数被调用!");

}

public MyClass2(int x){

name = "this is name for "+x;

addr = "this is addr for "+x;

System.out.println("构造函数被调用");

}

public String toString(){

return "name:\t"+name+"\naddr:\t"+addr;

}

public void readExternal(ObjectInput in) throws IOException,

ClassNotFoundException {

name = (String)in.readObject();

//addr = (String)in.readObject();

System.out.println("readExternal被调用!");

}

public void writeExternal(ObjectOutput out) throws IOException {

out.writeObject(name);

//out.writeObject(addr);

System.out.println("writeExternal被调用!");

}

}

public class JavaString {

public static void main(String args[]) throws FileNotFoundException, IOException, ClassNotFoundException{

File f= new File("logInfo.out");

f.createNewFile();

MyClass mc1 = new MyClass(1);

MyClass mc2 = new MyClass(2);

MyClass2 mc3 = new MyClass2(3);

MyClass2 mc4 = new MyClass2(4);

ObjectOutputStream o = new ObjectOutputStream(new FileOutputStream(f));

//依次写入三个对象

o.writeObject(mc1);

mc3.writeExternal(o);

o.writeObject(mc4);

o.close();

ObjectInputStream in = new ObjectInputStream(new FileInputStream(f));

System.out.println(mc1);

mc2 = (MyClass)in.readObject();

System.out.println(mc2);

mc3.readExternal(in);

System.out.println(mc3);

mc4 = (MyClass2)in.readObject();

System.out.println(mc4);

in.close();

}

}

结果

引用

构造函数被调用

构造函数被调用

构造函数被调用

构造函数被调用

writeExternal被调用!   //直接调用

writeExternal被调用!   //writeObject时调用

name:this is name for 1

addr:this is addr for 1

name:this is name for 1

addr:null

readExternal被调用!

name:this is name for 3

addr:this is addr for 3 //这里是修改 所以指保留原来

无参构造函数被调用!

readExternal被调用!

name:this is name for 4

addr:null               //这里是空直 没有transient修饰也可以实现

分享到:

18e900b8666ce6f233d25ec02f95ee59.png

72dd548719f0ace4d5f9bca64e1d7715.png

2010-05-05 21:13

浏览 1032

评论

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值