序列化就是一种操作,比如:当你需要将内存中的对象持久化存储到硬盘时,就需要序列化操作
下面这个例子就是把内存中的person对象持久化存储到硬盘中- import java.io.Serializable;
- public class Person implements Serializable
- {
- private String name;
- private int age;
-
- public Person(String name,int age)
- {
- this.name=name;
- this.age=age;
- }
-
-
- }
- [code]import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.ObjectOutputStream;
- import com.sun.org.apache.bcel.internal.generic.NEW;
- public class serializableDemo {
- /**
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- WriteObject();
- }
- public static void WriteObject() throws IOException
- {
- ObjectOutputStream ooStream=
- new ObjectOutputStream(new FileOutputStream("obj.txt"));
- ooStream.writeObject(new Person("zhangsan",18));
- ooStream.close();
- }
- }
复制代码 [/code] |