java序列化与ObjectOutputStream和ObjectInputStream的实例详解
程序员文章站
2024-03-02 13:18:16
java序列化与objectoutputstream和objectinputstream的实例详解
一个测试的实体类:
public class param...
java序列化与objectoutputstream和objectinputstream的实例详解
一个测试的实体类:
public class param implements serializable { private static final long serialversionuid = 5187074869820982336l; private integer param1; private string param2; public integer getparam1() { return param1; } public void setparam1(integer param1) { this.param1 = param1; } public string getparam2() { return param2; } public void setparam2(string param2) { this.param2 = param2; } }
测试:
public class main { public static void main(string[] args) throws exception { serializeparam(); param param = deserializeparam(); system.out.println(messageformat.format("param1={0},param2={1}", param.getparam1(), param.getparam2())); } /** * 将实体类序列化到本地 * @throws filenotfoundexception * @throws ioexception */ private static void serializeparam() throws filenotfoundexception, ioexception { param param = new param(); param.setparam1(123); param.setparam2("asdf"); objectoutputstream oo = new objectoutputstream(new fileoutputstream( new file("e:/param.txt") )); oo.writeobject(param); system.out.println("person对象序列化成功!"); oo.close(); } /** * 反序列化 * @return * @throws exception * @throws ioexception */ private static param deserializeparam() throws exception, ioexception { objectinputstream ois = new objectinputstream(new fileinputstream( new file("e:/param.txt"))); param param = (param) ois.readobject(); system.out.println("person对象反序列化成功!"); ois.close(); return param; } }
如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望通过本文能帮助到大家,谢谢大家对本站的支持!
推荐阅读
-
java序列化与ObjectOutputStream和ObjectInputStream的实例详解
-
Java序列化与反序列化的实例分析讲解
-
Java中的== 和equals()方法详解与实例
-
Java并发编程:CountDownLatch与CyclicBarrier和Semaphore的实例详解
-
Java中对象的序列化详解及实例
-
Java线程池的几种实现方法和区别介绍实例详解
-
Java中常见死锁与活锁的实例详解
-
实例详解java Struts2的配置与简单案例
-
java自带命令行工具jmap、jhat与jinfo的使用实例代码详解
-
java中 String和StringBuffer的区别实例详解