栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

序列换&反序列化

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

序列换&反序列化

序列化:把一个对象从内存中存放到硬盘中的过程
//所有要序列化的类都必须实现Serializable接口
		Dog d=new Dog("来福1","公",23);
		File file = new File("f:\图片\dog1");
		FileOutputStream fos = new FileOutputStream(file);
		BufferedOutputStream bos = new BufferedOutputStream(fos);
		ObjectOutputStream oos = new ObjectOutputStream(bos);
		oos.writeObject(d);

//关闭必须由内而外!
		oos.close();
		bos.close();
		fos.close();
		System.out.println("ok");
反序列化:将对象从硬盘介质中还原到内存中
		File file = new File("f:\图片\dog");
		FileInputStream fis = new FileInputStream(file);
		BufferedInputStream bis = new BufferedInputStream(fis);
		ObjectInputStream ois = new ObjectInputStream(bis);
		Dog d = (Dog)ois.readObject();
		System.out.println(d.getName());
		ois.close(); 
		bis.close();
		fis.close();

//保存1000只狗 
		ArrayList dogs = new ArrayList();
		for (int i = 0; i < 1000; i++) {
			Dog d = new Dog("旺财"+i, "公", i+1);
			dogs.add(d);
		}
		
		File file = new File("f:\图片\dogs.bak");
		FileOutputStream fos = new FileOutputStream(file);
		BufferedOutputStream bos = new BufferedOutputStream(fos);
		ObjectOutputStream oos = new ObjectOutputStream(bos);
		oos.writeObject(dogs);
		oos.close();
		bos.close();
		fos.close();
	

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/737233.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号