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
原创粉丝点击