JAVA 对象序列化
程序员文章站
2022-06-10 07:54:12
...
对象序列化:就是将一个对象转换为二进制的数据流,如果一个类的对象要想实现对象序列化,则对象所在的类必须实现Serializable接口。在此接口中没有任何的方法,此接口只是作为 一个标识,表示本类的对象具备了序列化的能力而已。
如果要想完成对象的序列化,则还要依靠ObjectOutputStream 类和 ObjectInputStream类,前者属于序列化操作,而后者属于反序列化操作
package org.serializabledemo; import java.io.Serializable; @SuppressWarnings("serial") public class Person implements Serializable { private String name; private int age; public Person(String name,int age){ this.name = name; this.age = age; } public String toString(){ return "姓名:"+this.name+";"+"年龄:"+this.age; } }
下面使用ObjectOutputStream 完成序列化的操作,对象被序列化之后,就可以通过ObjectInputStream 进行反序列化的操作
package org.serializabledemo; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; public class SerializableDemo { /** * @param args * @throws IOException * @throws ClassNotFoundException */ public static void main(String[] args) throws IOException, ClassNotFoundException { // TODO Auto-generated method stub Person per = new Person("张三",30); File file = new File ("d:\\1.txt"); ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file)); oos.writeObject(per); oos.close(); ObjectInputStream ois = new ObjectInputStream (new FileInputStream(file)); Person per1 = (Person)ois.readObject(); ois.close(); System.out.println(per1); } }
以上的操作实际上是将整个的对象进行了序列化的操作,如果现在假设类中的某个属性不希望被序列化的话,则要使用transient关键字声明
package org.serializabledemo; import java.io.Serializable; @SuppressWarnings("serial") public class PersonTransient implements Serializable { private transient String name; private transient int age; public PersonTransient(String name,int age){ this.name = name; this.age = age; } public String toString(){ return "姓名:"+this.name+";"+"年龄:"+this.age; } }
对多个对象进行序列化的操作 使用数组
package org.serializabledemo; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; public class SerializableArrayDemo { /** * @param args * @throws IOException * @throws FileNotFoundException */ public static void Ser(Object obj) throws Exception{ File file = new File ("d:\\1.txt"); ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file)); oos.writeObject(obj); oos.close(); } public static Object DSer(Object obj) throws Exception{ File file = new File ("d:\\1.txt"); ObjectInputStream ois = new ObjectInputStream (new FileInputStream(file)); Object obj_new = ois.readObject(); ois.close(); return obj_new; } public static void print(Person[] per){ for (Person p:per){ System.out.println(p); } } public static void main(String[] args) throws Exception { // TODO Auto-generated method stub Person per[] = {new Person("张三",30),new Person("王五",20),new Person("赵六",50)}; SerializableArrayDemo.Ser(per); Person per_new[] = (Person[])SerializableArrayDemo.DSer(per); SerializableArrayDemo.print(per_new); } }
下一篇: JAVA 对象序列化