读书人

初学者

发布时间: 2012-01-23 21:57:28 作者: rapoo

菜鸟求助~
关于Serializable接口 网上的说明看不太懂 求各位 给个通俗解释 最好有例子~~撒分~

[解决办法]
Object serialization 允许你将实现了Serializable接口的对象转换为字节序列,这些字节序列可以被完全存储以备以后重新生成原来的对象。serialization不但可以在本机做,而且可以经由网络操作(RMI)。这个好处是很大的----因为它自动屏蔽了操作系统的差异,字节顺序(用Unix下的c开发过网络编程的人应该知道这个概念)等。比如,在Window平台生成一个对象并序列化之,然后通过网络传到一台Unix机器上,然后可以在这台Unix机器上正确地重构这个对象。

通俗点讲就是实现这个借口的类得对象在传输前后可以被序列化和反序列化。得到原始的对象
[解决办法]

Java code
package myio;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.io.Serializable;import java.util.ArrayList;import java.util.Collection;import java.util.Iterator;public class TestPerson {    public static void main(String[] args) {    writeObjectToFile(new Person("张三", 20), "c:\\person.data");    Person p = readObjectFromFile("c:\\person.data");    System.out.println(p);            ArrayList<Person> persons = new ArrayList<Person>();    persons.add(new Person("李四", 21));    persons.add(new Person("王五", 22));        writeCollectionToFile(persons,"c:/persons");    ArrayList<Person> temppersons  = (ArrayList<Person>) readCollectionFromFile("c:/persons");    for(int i=0; i<temppersons.size(); i++){        System.out.println(temppersons.get(i));    }    }    /////////////////////////////////////////////////////////////////////////////////////////////////////////    private static <T> void writeObjectToFile(T t, String fileName) {// 将一个对象 写入文件    if (t == null)        return;// 空对象默认不写进去    ObjectOutputStream oos = null;    try {        oos = new ObjectOutputStream(new FileOutputStream(fileName));        oos.writeObject(t);        oos.flush();        oos.writeObject(null);// //写入结束标志方便读取(如果不写入,在读取的时候无法定位读取结束);        oos.flush();    } catch (Exception e) {    } finally {        try {        if (oos != null)            oos.close();        } catch (Exception e) {        }    }    }    private static <T> T readObjectFromFile(String fileName) {// 从文件中读出一个对象    ObjectInputStream ois = null;    try {        ois = new ObjectInputStream(new FileInputStream(fileName));        T t = (T) ois.readObject();        return t;    } catch (Exception e) {    } finally {        try {        if (ois != null)            ois.close();        } catch (Exception e) {        }    }    return null;    }    /////////////////////////////////////////////////////////////////////////////////////////////////////    private static <T> void writeCollectionToFile(Collection<T> collection,        String fileName) {// 将集合中的对象 写入文件    ObjectOutputStream oos = null;    try {        oos = new ObjectOutputStream(new FileOutputStream(fileName));        Iterator<T> iterator = collection.iterator();        while (iterator.hasNext()) {        T t = (T) iterator.next();        oos.writeObject(t);        oos.flush();        }        oos.writeObject(null);// //写入结束标志方便读取(如果不写入,在读取的时候无法定位读取结束);        oos.flush();    } catch (Exception e) {    } finally {        try {        if (oos != null)            oos.close();        } catch (Exception e) {        }    }    }    private static <T> Collection readCollectionFromFile(String fileName) {// 从文件中读取对象    // 返回一个集合    ObjectInputStream ois = null;    Collection<T> collection = new ArrayList<T>();    try {        ois = new ObjectInputStream(new FileInputStream(fileName));        Object tempObject = null;        T t = null;        while ((tempObject = ois.readObject()) != null) {        t = (T) tempObject;        collection.add(t);        }        return collection;    } catch (Exception e) {    } finally {        try {        if (ois != null)            ois.close();        } catch (Exception e) {        }    }    return null;    }}class Person implements Serializable {    private static final long serialVersionUID = 6213933727640755594L;    private String name;    private int id;    Person(String n, int i) {    name = n;    id = i;    }    public String getName() {    return name;    }    public int getId() {    return id;    }    public String toString() {    return "name:" + name + "  id:" + id;    }}/*output:name:张三  id:20name:李四  id:21name:王五  id:22 */ 

读书人网 >J2SE开发

热点推荐