java serializable 读写_JAVA(2)-Serializable-读写

序列化的读写磁盘细节很多。

上个最简单实例.

1.序列化的类要implements  Serializable

2.不参与序列化的用transient

3. 接口用 extends比如:public interface Strategy extends Serializable

知道以上3点基本够用了。

public class SerializeTest {

private Assert Assertions;

@Test

public void testwrite() {

try {

T t = new T();

File f = new File("/Users/Desktop/java/ideaPrj/a.dat");

FileOutputStream fos = new FileOutputStream(f);

ObjectOutputStream oos = new ObjectOutputStream(fos);

oos.writeObject(t);

oos.flush();

oos.close();

} catch (Exception e) {

e.printStackTrace();

}

}

@Test

public void testload() {

try {

File f = new File("/Users/Desktop/java/ideaPrj/a.dat");

FileInputStream fis = new FileInputStream(f);

ObjectInputStream ois = new ObjectInputStream(fis);

T t = (T) (ois.readObject());

System.out.println(t.m + " " + t.n);

// Assertions.assertEquals(4,t.m);

//Assertions.assertEquals(8,t.n);

//Assertions.assertEquals(3,t.a.weight);

ois.close();

} catch (Exception e) {

e.printStackTrace();

}

}

}

class T implements Serializable {

int m = 4;

int n=8;

// transient int n = 8;//不参与序列化

// Apple a =new Apple();

}

class Apple implements Serializable

{

int weight =3;

}

/*

public interface Strategy extends Serializable

*/

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值