Serializable接口实现对象序列化
来源:互联网 发布:淘宝流量计算公式 编辑:程序博客网 时间:2024/05/02 04:08
对象序列化就是把一个对象变为二进制的数据量的一种方法,通过对象序列化可以方便地实现对象的传输或存储.
class Person2 implements Serializable {private String name; // 声明name属性private int age; // 声明age属性public Person2(String name, int age) { // 通过构造设置内容this.name = name;this.age = age;}public String toString() { // 覆写toString()方法return "姓名:" + this.name + ";年龄:" + this.age;}};public class DemoDesign{public static void main(String args[]) throws Exception {File f = new File("D:" + File.separator + "test01.txt") ;// 定义保存路径ObjectOutputStream oos = null ;// 声明对象输出流OutputStream out = new FileOutputStream(f) ;// 文件输出流oos = new ObjectOutputStream(out) ;oos.writeObject(new Person2("张三",30)) ;// 保存对象oos.close() ;// 关闭ObjectInputStream ois = null ;// 声明对象输入流InputStream input = new FileInputStream(f) ;// 文件输入流ois = new ObjectInputStream(input) ;// 实例化对象输入流Object obj = ois.readObject() ;// 读取对象ois.close() ;// 关闭System.out.println(obj) ;}};
被Serializable接口声明的类的对象的内容都将被序列化,如果现在用户希望自己指定序列化的内容,则可以让一个类实现Externalizeable接口.
但这样做比较麻烦,所以可以在使用Serializable接口实现序列化操作时,对对象中的某个不希望被序列化的属性使用transient关键字进行声明.
public class Person implements Serializable{private transient String name ;// 声明name属性,但是此属性不被序列化private int age ;// 声明age属性public Person(String name,int age){// 通过构造设置内容this.name = name ;this.age = age ;}public String toString(){// 覆写toString()方法return "姓名:" + this.name + ";年龄:" + this.age ;}};<strong></strong>
public class SerDemo01{public static void main(String args[]) throws Exception{ser() ;dser() ;}public static void ser() throws Exception {File f = new File("D:" + File.separator + "test.txt") ;// 定义保存路径ObjectOutputStream oos = null ;// 声明对象输出流OutputStream out = new FileOutputStream(f) ;// 文件输出流oos = new ObjectOutputStream(out) ;oos.writeObject(new Person("张三",30)) ;// 保存对象oos.close() ;// 关闭}public static void dser() throws Exception {File f = new File("D:" + File.separator + "test.txt") ;// 定义保存路径ObjectInputStream ois = null ;// 声明对象输入流InputStream input = new FileInputStream(f) ;// 文件输入流ois = new ObjectInputStream(input) ;// 实例化对象输入流Object obj = ois.readObject() ;// 读取对象ois.close() ;// 关闭System.out.println(obj) ;}};
序列化一组对象:
public class SerDemo02{public static void main(String args[]) throws Exception{Person per[] = {new Person("张三",30),new Person("李四",31),new Person("王五",32)} ;ser(per) ;Object o[] = (Object[])dser() ;for(int i=0;i<o.length;i++){Person p = (Person)o[i] ;System.out.println(p) ;}}public static void ser(Object obj[]) throws Exception {File f = new File("D:" + File.separator + "test.txt") ;// 定义保存路径ObjectOutputStream oos = null ;// 声明对象输出流OutputStream out = new FileOutputStream(f) ;// 文件输出流oos = new ObjectOutputStream(out) ;oos.writeObject(obj) ;// 保存对象oos.close() ;// 关闭}public static Object[] dser() throws Exception {File f = new File("D:" + File.separator + "test.txt") ;// 定义保存路径ObjectInputStream ois = null ;// 声明对象输入流InputStream input = new FileInputStream(f) ;// 文件输入流ois = new ObjectInputStream(input) ;// 实例化对象输入流Object obj[] = (Object[])ois.readObject() ;// 读取对象ois.close() ;// 关闭return obj ;}};
3 0
- Serializable接口实现对象序列化
- 对象的序列化实现Serializable 接口和Externalizable接口
- Serializable对象序列化接口
- 实现serializable序列化接口
- 对象的序列化,需要实现Serializable 接口
- 对象的序列化与Serializable 接口
- java.io.Serializable接口实现序列化
- java序列化1[实现Serializable接口]
- Java序列化(实现Serializable接口)作用
- 为什么要实现序列化接口Serializable
- 序列化接口Serializable
- Serializable 序列化接口
- Android 对象序列化 Java中实现Serializable序列化与Android特有的实现Parceable接口序列化操作
- 用java原生接口Serializable实现对单例对象的序列化
- JAVA 源码浅解析之为什么对象序列化要实现Serializable接口
- 对象序列化 Serializable
- 对象序列化Serializable
- 理解Java对象序列化——Serializable接口
- Ant构建文件解析(转)
- 工作笔记 NTP fix bug
- 专业的太阳镜范围
- 锁及事务的高级使用
- 系统相册分析(GallerPicker)
- Serializable接口实现对象序列化
- NFC开发总结
- 在故障排除过程中要注意的事
- glClearDepth
- Hadoop 2.2.0
- Reverse Words in a String
- 使用soundPool播放音频
- Linux 之 /etc/profile、~/.bash_profile 等几个文件的执行过程
- c++ STL 库编程 Vector