对象序列化

来源:互联网 发布:java快速排序方法 编辑:程序博客网 时间:2024/06/10 03:57

如何对象序列化呢?
ObjectInputStream 输入,读取,反序列化
ObjectOutputStream 输出,写入,序列化

请看如下示例代码:

package a10;import java.io.BufferedInputStream;import java.io.BufferedOutputStream;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;import java.util.Date;public class Test109 {    public static void main(String[] args) {        Date date = new Date();        File file  = new File("e:\\b.txt");        try {            /**             *  序列化             */            FileOutputStream fos = new FileOutputStream(file);            BufferedOutputStream bos = new BufferedOutputStream(fos);//其实可以不用buffered这个中间过程的            ObjectOutputStream oos = new ObjectOutputStream(bos);            oos.writeObject(date);            oos.close();            bos.close();            fos.close();            /**             * 反序列化             */            FileInputStream fis = new FileInputStream(file);            BufferedInputStream bis  = new BufferedInputStream(fis);            ObjectInputStream ois = new ObjectInputStream(bis);            System.out.println(ois.readObject());            ois.close();            bis.close();            fis.close();        } catch (FileNotFoundException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        } catch (ClassNotFoundException e) {            e.printStackTrace();        }    }}

这样就将对象保存到了b.txt文件中,这样无论将文件移植到哪里,都可以通过反序列化技术,将对象以及对象信息全部都拿出来,感觉棒棒哒~

运行结果:

Wed Aug 12 15:49:46 CST 2015

若想格式化日期啊,可以看另一篇文章:“仿cmd下的dir命令”
其中会演示如何通过使用:SimpleDateFormat(格式化日期) 以及 DecimalFormat(格式化数字)类来进行格式化。

但是,这里需要注意的是Date这个类实现了Serializable接口,
可是自己写的类若要想保存对象序列化的话,就实现Serializable接口才可以,因为:
只要对象实现了Serializable接口(该接口仅是一个标记接口,不包含任何方法,对象的序列化就会变得非常简单)

请看如何序列化自己的Dog类呢?

package a10;import java.io.Serializable;public class Dog implements Serializable{    private String name;     private int age;    public Dog(String name, int age) {        super();        this.name = name;        this.age = age;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public int getAge() {        return age;    }    public void setAge(int age) {        this.age = age;    }}

还有测试类:

package a10;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 Test110 {    public static void main(String[] args) {        Dog d = new Dog("旺财", 19);        File file = new File("e:\\c.ser");        FileOutputStream fos;        try {            fos = new FileOutputStream(file);            ObjectOutputStream oos = new ObjectOutputStream(fos);            oos.writeObject(d);            oos.close();            fos.close();            FileInputStream fis = new FileInputStream(file);            ObjectInputStream ois = new ObjectInputStream(fis);            Dog dd = (Dog)ois.readObject();            ois.close();            fis.close();            System.out.println(dd.getName()+"\t"+dd.getAge());        } catch (FileNotFoundException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        } catch (ClassNotFoundException e) {            e.printStackTrace();        }    }}

运行的结果:

旺财  19

嘿嘿,就是简单的implements Serializable接口就妥妥滴了,(^__^) 嘻嘻!

0 0
原创粉丝点击