Java编程思想:序列化基础部分
2024-09-01 02:19:01
import java.io.*; import java.util.Date; import java.util.Random; public class Test { public static void main(String[] args){ // Worm.test(); // Blip.test(); // Login.test(); SerialCtl.test(); } } class Data implements Serializable { private int n; public Data(int n) { this.n = n; } public String toString() { return Integer.toString(n); } } class Worm implements Serializable { private static final int SIZE = 1024; private static Random random = new Random(47); private Data[] datas = new Data[]{ new Data(random.nextInt(10)), new Data(random.nextInt(10)), new Data(random.nextInt(10)) }; private Worm next; private char c; public Worm(int i, char c) { System.out.println("Worm Constructor: " + i); this.c=c; if (--i > 0) { next=new Worm(i,(char)(c+1)); } } public Worm() { System.out.println("Default Constructor"); } public String toString() { StringBuilder sb = new StringBuilder(":"); sb.append("c"); sb.append("("); for (Data data : datas) { sb.append(data); } sb.append(")"); if (next != null) { sb.append(next); } return sb.toString(); } public static void test() { try{ Worm w = new Worm(6, 'a'); System.out.println("w = "+w); /* //创建ObjectOutputStream用于储存序列化结果 ObjectOutputStream out = new ObjectOutputStream( new FileOutputStream("worm.out")); out.writeObject("Worm Storage\n"); out.writeObject(w); out.close(); //创建ObjectInputStream用于读取序列化数据 ObjectInputStream in = new ObjectInputStream( new FileInputStream("worm.out")); //从序列化数据恢复时,需要按照序列化顺序 String s = (String)in.readObject(); Worm w2 = (Worm)in.readObject(); System.out.println(s+"w2 = "+w); */ //测试,将序列化数据储存在ByteArrayOutputStream中 //这个地方这样写,不便于后面操作 /* ObjectOutputStream out2 = new ObjectOutputStream( new ByteArrayOutputStream()); */ ByteArrayOutputStream bs = new ByteArrayOutputStream(); ObjectOutputStream out2 = new ObjectOutputStream(bs); out2.writeObject("Worm Storage In Bytes...\n"); out2.writeObject(w); out2.flush(); ObjectInputStream in2 = new ObjectInputStream( new ByteArrayInputStream(bs.toByteArray())); String s2 = (String)in2.readObject(); Worm w3 = (Worm)in2.readObject(); System.out.println(s2 + "w3 = "+w3); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } } } /* Serializable序列化时,对象完全以它存储的二进制为为基础来构造,而不调用构造器 Externnlizable序列化时,所有普通的默认构造器都会被调用(包括字段定义时的初始 化),然后调用readExternal() */ class Blip implements Externalizable { private int i; private String s; public Blip() { System.out.println("Constructor: Blip()"); } public Blip(String s, int i) { System.out.println("Constructor: Blip(String s, int i)"); this.s=s; this.i=i; } public String toString() { return s + i; } @Override public void writeExternal(ObjectOutput out) throws IOException { System.out.println("Blip.writeExternal..."); out.writeObject(s); //这个地方写了一个BUG,用Object会造成自动装箱,导致readInt()读不出来结果 //out.writeObject(i); out.writeInt(i); } @Override public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { System.out.println("Blip.readExternal..."); this.s = (String)in.readObject(); this.i = in.readInt(); } public static void test() { try{ Blip b = new Blip("A String...", 47); System.out.println(b); //创建输出流 ObjectOutputStream out = new ObjectOutputStream( new FileOutputStream("Blip.out")); out.writeObject(b); out.close(); //创建输入流 ObjectInputStream in = new ObjectInputStream( new FileInputStream("Blip.out")); Blip b2 = (Blip)in.readObject(); System.out.println(b2); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } } } /* 测试 Transient关键字 */ class Login implements Serializable { private Date date = new Date(); private String username; private transient String password; public Login(String name, String password) { this.username = name; this.password=password; } public String toString() { return "Login info:"+ "\n username:"+username+ "\n password:"+password+ "\n date:"+date; } public static void test() { try{ Login l = new Login("zhangshan", "123456"); System.out.println("Login l = "+l); //创建输出流 ObjectOutputStream out = new ObjectOutputStream( new FileOutputStream("login.out")); out.writeObject(l); //创建输入流 ObjectInputStream in = new ObjectInputStream( new FileInputStream("login.out")); Login m = (Login)in.readObject(); System.out.println("Login m = "+m); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } } } /* Externalizable的替代方案,非常混乱的一个方案,没有研究的价值 */ class SerialCtl implements Serializable { private String username; private transient String password; public SerialCtl(String name, String password) { this.username=name; this.password=password; } public String toString() { return username+"\n"+password; } private void writeObject(ObjectOutputStream outputStream) throws IOException{ outputStream.defaultWriteObject(); outputStream.writeObject(password); } private void readObject(ObjectInputStream inputStream) throws IOException,ClassNotFoundException{ inputStream.defaultReadObject(); password=(String)inputStream.readObject(); } public static void test() { SerialCtl sc = new SerialCtl("Test1", "Test2"); System.out.println("Before:\n"+sc); try{ //创建输出流 ObjectOutputStream out = new ObjectOutputStream( new FileOutputStream("SerialCtl.out")); out.writeObject(sc); //创建输入流 ObjectInputStream in = new ObjectInputStream( new FileInputStream("SerialCtl.out")); SerialCtl sc2 = (SerialCtl)in.readObject(); System.out.println("Now:\n"+sc2); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } } }
最新文章
- java异常处理(父子异常的处理)
- android的程序运行数据存放在哪里?
- java操作MySQL数据库(插入、删除、修改、查询、获取所有行数)
- javascript 变量解析
- EF——使用Data Annotations和Fluent API配置数据库的映射配置 02.01(转)
- xml--小结②XML的基本语法
- quick-cocos2dx学习笔记
- 实现标签的添加与删除(tags)
- centos 7 最小安装后 安装FTP服务器 vsftp
- layer ui插件显示tips时,修改字体颜色
- Python爬虫与一汽项目【综述】
- 宝塔搭建laravel所需要的lnmp环境linux-nginx-mysql-php-composer-git
- 48 【golang】json的效率
- 0_Simple__simpleSurfaceWrite
- C#中的特性 (Attribute) 入门 (一)
- YARN中用的作业调度算法:DRF(Dominant Resource Fairness)
- [Windows]_[删除非空文件夹的注意要点]
- Java 完美判断字符串中中文字符【中文符号】
- java压缩与解压
- OpenCV 3.2 Viz 3D可视化
热门文章
- fileapi.h里的API函数(包括LockFileEx和FindFirstChangeNotification函数)
- Android 开发中,as或者idea对gradle的使用
- DOTNET CORE DATETIME在LINUX与WINDOWS时间不一致
- Sysinternals套件2016年11月更新发布,诸多工具被更新
- visual studio添加docker支持简记
- 插件化一(android)
- qt开发的小软件,可以递归转换文件编码(qt为了防止内存泄露所做的保护机制)
- 【原创】ABAP根据文件路径获取文件所在目录
- GCC链接库的一个坑:动态库存在却提示未定义动态库的函数
- kubernetes使用http rest api访问集群之使用postman工具访问 apiserver