Java编程思想:序列化深层部分
2024-10-02 05:20:22
import java.io.*; import java.util.ArrayList; import java.util.List; public class Test { public static void main(String[] args){ // MyWorld.test(); ATest.test(); } } /* 只要把任何对象序列化到单一的流中,就可以恢复出与我们写时一样的对象网,并且没有任何 意外重复复制的对象。 */ class House implements Serializable { // } class Animal implements Serializable { private String name; private House house; public Animal(String name, House house) { this.name=name; this.house=house; } public String toString() { return name+" ["+house+"] "; } } class MyWorld { public static void test() { try { /* 问题: 1.animals中每个animal都使用同一个house,序列化时,house会不会被序列多个 */ House house = new House(); List<Animal> animals = new ArrayList<>(); animals.add(new Animal("A", house)); animals.add(new Animal("B", house)); animals.add(new Animal("C", house)); System.out.println("animals:" + animals); //创建输出流 ByteArrayOutputStream bstream = new ByteArrayOutputStream(); ObjectOutputStream out1 = new ObjectOutputStream(bstream); out1.writeObject(animals); out1.writeObject(animals); out1.flush(); ByteArrayOutputStream bstream2 = new ByteArrayOutputStream(); ObjectOutputStream out2 = new ObjectOutputStream(bstream2); out2.writeObject(animals); out2.flush(); //创建输入流 ObjectInputStream in1 = new ObjectInputStream( new ByteArrayInputStream(bstream.toByteArray())); ObjectInputStream in2 = new ObjectInputStream( new ByteArrayInputStream(bstream2.toByteArray())); List animals1 = (List)in1.readObject(), animals2 = (List)in1.readObject(), animals3 = (List)in2.readObject(); System.out.println("animals1:" + animals1); System.out.println("animals2:" + animals2); System.out.println("animals3:" + animals3); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } } } /* 问题: 书中说,我们写出第一个对象和写出最后一个对象期间改变了这些对象的状态,但是又说 只要我们序列化到同一个流中,就能恢复和写时一样的对象网。问题是,如果一个对象我 第一写入时是x值,我改变了这个值为y,又写一次,那我恢复时,得到的对象的值是多少 呢? 输出结果: a1.a: 10 a2.a: 10 a1:A@568db2f2 a2:A@568db2f2 分析: 我不知道这是为什么哦,感觉很奇怪,第二次的操作跟没操作一样的 */ class A implements Serializable{ private int a; public A(int a) { this.a = a; } public int getA() { return a; } public void setA(int a) { this.a = a; } } class ATest{ public static void test() { A a = new A(10); try{ //创建输出流 ByteArrayOutputStream bstream = new ByteArrayOutputStream(); ObjectOutputStream out = new ObjectOutputStream(bstream); out.writeObject(a); //改变a的值 a.setA(100); //再次写入 out.writeObject(a); out.flush(); //创建输入流 ObjectInputStream in = new ObjectInputStream( new ByteArrayInputStream(bstream.toByteArray())); A a1 = (A)in.readObject(); A a2 = (A)in.readObject(); System.out.println("a1.a: "+a1.getA()); System.out.println("a2.a: "+a2.getA()); System.out.println("a1:"+a1); System.out.println("a2:"+a2); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } } } /* 如果我们想保存状态,最安全的做法是将其作为“原子”操作进行序列化。如果我们 序列化了某些东西,再去做其他的一些工作,再来序列化更多的东西,如此等等, 那么将无法安全的保存系统状态。取而代之的是,将构成系统状态的所有对象都置 入单一容器内,并在一个操作中将该容器直接写出。然后同样只需要一次方法调用 ,即可以恢复状态 */
最新文章
- C# Invoke或者BeginInvoke的使用
- hibernate学习-HibernateDemo
- Stop sucking,Become awesome.这一年我做到了么
- Spring3:AOP
- 物理引擎-Physx的源代码去哪里找
- What is the difference between routine , method , procedure , function ? please explain it with example?
- IE关闭兼容性视图
- linux rpm 安装包 信息查询
- JS让input按钮不能点击
- 一表格简单明了区分BI和大数据
- 百度web前端面试2015.10.18
- 教你50招提升ASP.NET性能(十五):解决性能问题时不要低估UI的价值
- lastIndexOf方法——获取字符最后的索引
- Apache和PHP的优化
- HttpSesstionActivationLIstener示例
- ubuntu-16.04(linux)使用Reaver爆破wifi密码(路由器的WPS功能漏洞)
- Node.js 异步异闻录
- 07_Python变量内存地址、小数据池
- ASP.NET CORE的Code Fist后Models更改了怎么办?
- Java 什么是线程安全
热门文章
- 使用IntelliJ IDEA开发SpringMVC网站(四)用户管理
- Portal for ArcGIS 资源承载数据类型
- 【Windows10 ;IoT开发系列】Powershell命令行实用程序
- 安装 ;VirtualBox ;出现回滚,无法安装及解决方法
- 如何替换Windows的Shell(即explorer.exe)
- Android零基础入门第76节:Activity数据保存和横竖屏切换
- Qt https 用户认证authenticationRequired()
- qtextedit中的光标问题(通过调用repaint去掉Focus的阴影)
- 区块狗开发可以做出APP吗
- 01-pymysql模块的安装