手机版
你好,游客 登录 注册
背景:
阅读新闻

Java对象的序列化

[日期:2014-10-26] 来源:Linux社区  作者:Linux [字体:大 中 小]

4.Java序列化与反序列话的三种格式(默认,XML,json)

定义了类Person,并声明其可以序列化:

public class Person implements Serializable {
 
    private static final long serialVersionUID = 1L;
   
    private String name;;
    private int age;
    public Person(){
       
    }
    public Person(String str,int n){
        System.out.println("Inside Person's Constructor");
        name = str;
        age = n;
    }
    public String getName() {
        return name;
    }
    public int getAge() {
        return age;
    }
}

下面是三种格式的转换的代码:

1.默认格式:

public class SerializeToFlatFile {
    public static void main(String[] args) {
        SerializeToFlatFile ser = new SerializeToFlatFile();
        ser.savePerson();
        ser.restorePerson();
    }
   
    public void savePerson(){
        Person myPerson = new Person("Jay", 24);
        try{
            FileOutputStream fos = new FileOutputStream("E:\\person.txt");
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            System.out.println("Person--Jay,24---Written");
           
            oos.writeObject(myPerson);
            oos.flush();
            oos.close();
        }catch(Exception e){
            e.printStackTrace();
        }
    }
   
    @SuppressWarnings("resource")
    public void restorePerson(){
        try{
            FileInputStream fls = new FileInputStream("E:\\person.txt");
            ObjectInputStream ois = new ObjectInputStream(fls);
           
            Person myPerson = (Person)ois.readObject();
            System.out.println("\n---------------------\n");
            System.out.println("Person --read:");
            System.out.println("Name is:"+myPerson.getName());
            System.out.println("Age is :"+myPerson.getAge());
           
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}

2.XML格式:

public class SerializeXML {
 
    public static void main(String[] args) {
        SerializeXML ser = new SerializeXML();
        ser.serializeToXml();
        ser.deSerializeFromXml();
    }
    public void serializeToXml(){
        Person[] myPersons = new Person[2];
        myPersons[0] = new Person("Jay", 24);
        myPersons[1] = new Person("Tom", 23);
       
        XStream xStream = new XStream();
        xStream.alias("Person", Person.class);
        try{
            FileOutputStream foStream = new FileOutputStream("E:\\persons.xml");
            xStream.toXML(myPersons,foStream);
        }catch(Exception e){
            e.printStackTrace();
        }
    }
    public void deSerializeFromXml(){
        XStream xStream = new XStream();
        xStream.alias("Person", Person.class);
        Person[] myPersons = null;
        try{
            FileInputStream flStream = new FileInputStream("E:\\persons.xml");
            myPersons = (Person[])xStream.fromXML(flStream);
            if(myPersons!=null){
                for(Person person:myPersons){
                    System.out.println(person.getName());
                    System.out.println(person.getAge());
                }
            }
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}

3.json格式:

public class SerializeJSON {
 
    public static void main(String[] args) {
        SerializeJSON serializeJSON = new SerializeJSON();
        serializeJSON.writeJSON();
        serializeJSON.readJSON();
    }
 
    public void writeJSON(){
        XStream xStream = new XStream(new JettisonMappedXmlDriver());
        Person person = new Person("geniushehe", 16);
        try {
            FileOutputStream fos = new FileOutputStream("E:\\json.js");
            xStream.setMode(XStream.NO_REFERENCES);
            xStream.alias("Person", Person.class);
            xStream.toXML(person, fos);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
    public void readJSON(){
        XStream xStream = new XStream(new JettisonMappedXmlDriver());
        Person person = null;
        try {
            FileInputStream fis = new FileInputStream("E:\\json.js");
            xStream.setMode(XStream.NO_REFERENCES);
            xStream.alias("Person", Person.class);
            person = (Person)xStream.fromXML(fis);
            System.out.println(person.getName());
            System.out.println(person.getAge());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
}

本文永久更新链接地址:http://www.linuxidc.com/Linux/2014-10/108466.htm

linux
相关资讯       Java序列化  对象序列化 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款