Java实现对象序列化的两种方式Demo

网友投稿 252 2022-11-21

Java实现对象序列化的两种方式Demo

package com.luhao.serializable; import java.io.Externalizable; import java.io.FileInputStream; 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.io.Serializable; import org.junit.Test; public class Test06_Serializable { //Java中如何实现对象的序列化 //1.实现Serializable接口 //2.实现Externaliable接口,并实现读取写入方法,写入指定的属性 //区别:Externalizabel反序列化创建对象的时候会调用构造器,而Serializable不需要 //Serializable中transient修饰的方法不会被序列话(例子略),除非包含两个方法writeObj和ReadObj,因为序列化的时候java会寻找这两个方法的 @Test public void testReadAndWrite() throws Exception { TestSerializable t1 = new TestSerializable(); t1.setUserName("luhao"); t1.setAge(26); ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("e:\\test")); out.writeObject(t1); System.out.println("写入成功"); ObjectInputStream ins = new ObjectInputStream(new FileInputStream("e:\\test")); System.out.println("正在读取"); System.out.println(ins.readObject()); ins.close(); out.close(); } @Test public void testReadAndWrite2() throws Exception { TestExternaliable t1 = new TestExternaliable(); t1.setUserName("luhao"); t1.setAge(26); ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("e:\\test")); out.writeObject(t1); System.out.println("写入成功"); ObjectInputStream ins = new ObjectInputStream(new FileInputStream("e:\\test")); System.out.println("正在读取"); System.out.println(ins.readObject()); ins.close(); out.close(); } } class TestSerializable implements Serializable{ private static final long serialVersionUID = 2817558343900452926L; private String userName; private int age; public TestSerializable() { System.out.println("构造器没有被调用?"); } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "TestSerializable [userName=" + userName + ", age=" + age + "]"; } private void writeObject(ObjectOutputStream out) throws IOException { out.defaultWriteObject(); out.writeInt(age); } private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { in.defaultReadObject(); age = in.readInt(); age = 666; } } class TestExternaliable implements Externalizable{ private String userName; private int age; public TestExternaliable() { System.out.println("构造器被调用了?"); } @Override public void writeExternal(ObjectOutput out) throws IOException { out.writeObject(userName); out.writeObject(age); } @Override public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { userName = (String) in.readObject(); age = (int)in.readObject(); } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "TestExternaliable [userName=" + userName + ", age=" + age + "]"; } }

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:python如何求水仙花数
下一篇:两款音频接口电路解析
相关文章

 发表评论

暂时没有评论,来抢沙发吧~