2016/5/26
2016-05-26 20:53
357 查看
对象的序列化,(数据库存储一个对象时可用)
import java.io.*; public class Test { public static void main(String[] args) { Dog d = new Dog(); d.setS("GG"); d.setT("dadada"); System.out.println("d的i = "+d.getI()+" d的s = "+d.getS()+"d的t = "+d.getT()); Dog g; try { FileOutputStream fos = new FileOutputStream("D:/java/Dog.ser");//标准正斜杠/在哪里都可以用 ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeObject(d); oos.flush(); oos.close(); FileInputStream fis = new FileInputStream("D:/java/Dog.ser"); ObjectInputStream ois = new ObjectInputStream(fis); g = (Dog)ois.readObject(); ois.close(); System.out.println("g的i = "+g.getI()+" g的s = "+g.getS()+"g的t = "+g.getT()); System.out.println("d的i = "+d.getI()+" d的s = "+d.getS()+"d的t = "+d.getT()); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } class Dog implements Serializable { public int getI() { return i; } public void setI(int i) { this.i = i; } public String getS() { return s; } public void setS(String s) { this.s = s; } /** * */ private static final long serialVersionUID = -4952231124459988866L; private int i = 8; private String s = "ss"; transient String t = "tt"; public String getT() { return t; } public void setT(String t) { this.t = t; } }运行结果:
d的i = 8 d的s = GGd的t = dadada g的i = 8 g的s = GGg的t = null d的i = 8 d的s = GGd的t = dadada
相关文章推荐
- 【转】js 关键字 in 的使用方法
- lua 源码阅读顺序
- PHP - 如何解决中文乱码
- PHP - 如何解决中文乱码
- Vuejs学习(一)简要的TODOList
- 河南省第六届大学生程序设计竞赛 F Card Trick
- 搬瓦工VPS(Centos6_x86)+LNMP+WordPress搭建个人博客/网站
- POJ 1067 威佐夫博弈
- sdut 3272 山理工第七届校赛--字符的变化
- POJ 1067 威佐夫博弈
- 用JS生成MIDI文件(附代码)
- 谁的青春不迷茫
- 【操作系统】linux创建子进程--fork()方法
- GridView属性大全
- Touch panel DTS 分析(MSM8994平台,Atmel 芯片)
- 数组中增加或者删除元素
- Android之Adapter用法总结
- 深入浅出linux内核源代码之双向链表list_head(下):
- Struts2中关于"There is no Action mapped for namespace / and action name"的总结
- RESTful API 设计指南