JAVA序列化与反序列化三种格式存取(默认格式、XML格式、JSON格式)_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > JAVA序列化与反序列化三种格式存取(默认格式、XML格式、JSON格式)

JAVA序列化与反序列化三种格式存取(默认格式、XML格式、JSON格式)

 2013/8/31 16:09:21  piaolingluo  程序员俱乐部  我要评论(0)
  • 摘要:什么是序列化java中的序列化(serialization)机制能够将一个实例对象的状态信息写入到一个字节流中,使其可以通过socket进行传输、或者持久化存储到数据库或文件系统中;然后在需要的时候,可以根据字节流中的信息来重构一个相同的对象。序列化机制在java中有着广泛的应用,EJB、RMI等技术都是以此为基础的。正确使用序列化机制一般而言,要使得一个类可以序列化,只需简单实现java.io.Serializable接口即可(还要实现无参数的构造方法)。该接口是一个标记式接口
  • 标签:Java JSON XML JS 序列化
什么是序列化?
java中的序列化(serialization)机制能够将一个实例对象的状态信息写入到一个字节流中,使其可以通过socket进行传输、或者持久化存储到数据库或文件系统中;然后在需要的时候,可以根据字节流中的信息来重构一个相同的对象。序列化机制在java中有着广泛的应用,EJB、RMI等技术都是以此为基础的。?

正确使用序列化机制?
一般而言,要使得一个类可以序列化,只需简单实现java.io.Serializable接口即可(还要实现无参数的构造方法)。该接口是一个标记式接口,它本身不包含任何内容,实现了该接口则表示这个类准备支持序列化的功能。如下例定义了类Person,并声明其可以序列化。 上面两段来源:http://lionheart.iteye.com/blog/139540 ? ?

?

[java]?view plaincopy ?
    class="dp-j" style="border: none; background-color: #ffffff; color: #5c5c5c; margin-bottom: 1px !important; margin-left: 45px !important;">
  1. public?class?Person?implements?Serializable?{??
  2. ??
  3. ????private?static?final?long?serialVersionUID?=?1L;??
  4. ??????
  5. ????private?String?name;;??
  6. ????private?int?age;??
  7. ????public?Person(){??
  8. ??????????
  9. ????}??
  10. ????public?Person(String?str,int?n){??
  11. ????????System.out.println("Inside?Person's?Constructor");??
  12. ????????name?=?str;??
  13. ????????age?=?n;??
  14. ????}??
  15. ????public?String?getName()?{??
  16. ????????return?name;??
  17. ????}??
  18. ????public?int?getAge()?{??
  19. ????????return?age;??
  20. ????}??
  21. }??



?

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

1.默认格式.

?

[java]?view plaincopy ?
  1. public?class?SerializeToFlatFile?{??
  2. ????public?static?void?main(String[]?args)?{??
  3. ????????SerializeToFlatFile?ser?=?new?SerializeToFlatFile();??
  4. ????????ser.savePerson();??
  5. ????????ser.restorePerson();??
  6. ????}??
  7. ??????
  8. ????public?void?savePerson(){??
  9. ????????Person?myPerson?=?new?Person("Jay",?24);??
  10. ????????try{??
  11. ????????????FileOutputStream?fos?=?new?FileOutputStream("E:\\person.txt");??
  12. ????????????ObjectOutputStream?oos?=?new?ObjectOutputStream(fos);??
  13. ????????????System.out.println("Person--Jay,24---Written");??
  14. ??????????????
  15. ????????????oos.writeObject(myPerson);??
  16. ????????????oos.flush();??
  17. ????????????oos.close();??
  18. ????????}catch(Exception?e){??
  19. ????????????e.printStackTrace();??
  20. ????????}??
  21. ????}??
  22. ??????
  23. ????@SuppressWarnings("resource")??
  24. ????public?void?restorePerson(){??
  25. ????????try{??
  26. ????????????FileInputStream?fls?=?new?FileInputStream("E:\\person.txt");??
  27. ????????????ObjectInputStream?ois?=?new?ObjectInputStream(fls);??
  28. ??????????????
  29. ????????????Person?myPerson?=?(Person)ois.readObject();??
  30. ????????????System.out.println("\n---------------------\n");??
  31. ????????????System.out.println("Person?--read:");??
  32. ????????????System.out.println("Name?is:"+myPerson.getName());??
  33. ????????????System.out.println("Age?is?:"+myPerson.getAge());??
  34. ??????????????
  35. ????????}catch(Exception?e){??
  36. ????????????e.printStackTrace();??
  37. ????????}??
  38. ????}??
  39. }??

?

输出结果: sr test.serializable.Person ? ? ? I ageL namet Ljava/lang/String;xp ? t Jay

?

2.XML格式

?

[java]?view plaincopy ?
  1. //参考:http://www.cnblogs.com/bluesky5304/archive/2010/04/07/1706061.html??
  2. public?class?SerializeXML?{??
  3. ??
  4. ????public?static?void?main(String[]?args)?{??
  5. ????????SerializeXML?ser?=?new?SerializeXML();??
  6. ????????ser.serializeToXml();??
  7. ????????ser.deSerializeFromXml();??
  8. ????}??
  9. ????public?void?serializeToXml(){??
  10. ????????Person[]?myPersons?=?new?Person[2];??
  11. ????????myPersons[0]?=?new?Person("Jay",?24);??
  12. ????????myPersons[1]?=?new?Person("Tom",?23);??
  13. ??????????
  14. ????????XStream?xStream?=?new?XStream();??
  15. ????????xStream.alias("Person",?Person.class);??
  16. ????????try{??
  17. ????????????FileOutputStream?foStream?=?new?FileOutputStream("E:\\persons.xml");??
  18. ????????????xStream.toXML(myPersons,foStream);??
  19. ????????}catch(Exception?e){??
  20. ????????????e.printStackTrace();??
  21. ????????}??
  22. ????}??
  23. ????public?void?deSerializeFromXml(){??
  24. ????????XStream?xStream?=?new?XStream();??
  25. ????????xStream.alias("Person",?Person.class);??
  26. ????????Person[]?myPersons?=?null;??
  27. ????????try{??
  28. ????????????FileInputStream?flStream?=?new?FileInputStream("E:\\persons.xml");??
  29. ????????????myPersons?=?(Person[])xStream.fromXML(flStream);??
  30. ????????????if(myPersons!=null){??
  31. ????????????????for(Person?person:myPersons){??
  32. ????????????????????System.out.println(person.getName());??
  33. ????????????????????System.out.println(person.getAge());??
  34. ????????????????}??
  35. ????????????}??
  36. ????????}catch(Exception?e){??
  37. ????????????e.printStackTrace();??
  38. ????????}??
  39. ????}??
  40. }??

?

输出结果:

?

[html]?view plaincopy ?
  1. <Person-array>??
  2. ??<Person>??
  3. ????<name>Jay</name>??
  4. ????<age>24</age>??
  5. ??</Person>??
  6. ??<Person>??
  7. ????<name>Tom</name>??
  8. ????<age>23</age>??
  9. ??</Person>??
  10. </Person-array>??

?


3.JSON格式

?

[java]?view plaincopy ?
  1. //参考:http://www.cnblogs.com/hoojo/archive/2011/04/22/2025197.html??
  2. public?class?SerializeJSON?{??
  3. ??
  4. ????public?static?void?main(String[]?args)?{??
  5. ????????SerializeJSON?serializeJSON?=?new?SerializeJSON();??
  6. ????????serializeJSON.writeJSON();??
  7. ????????serializeJSON.readJSON();??
  8. ????}??
  9. ??
  10. ????public?void?writeJSON(){??
  11. ????????XStream?xStream?=?new?XStream(new?JettisonMappedXmlDriver());??
  12. ????????Person?person?=?new?Person("geniushehe",?16);??
  13. ????????try?{??
  14. ????????????FileOutputStream?fos?=?new?FileOutputStream("E:\\json.js");??
  15. ????????????xStream.setMode(XStream.NO_REFERENCES);??
  16. ????????????xStream.alias("Person",?Person.class);??
  17. ????????????xStream.toXML(person,?fos);??
  18. ????????}?catch?(FileNotFoundException?e)?{??
  19. ????????????e.printStackTrace();??
  20. ????????}??
  21. ????}??
  22. ????public?void?readJSON(){??
  23. ????????XStream?xStream?=?new?XStream(new?JettisonMappedXmlDriver());??
  24. ????????Person?person?=?null;??
  25. ????????try?{??
  26. ????????????FileInputStream?fis?=?new?FileInputStream("E:\\json.js");??
  27. ????????????xStream.setMode(XStream.NO_REFERENCES);??
  28. ????????????xStream.alias("Person",?Person.class);??
  29. ????????????person?=?(Person)xStream.fromXML(fis);??
  30. ????????????System.out.println(person.getName());??
  31. ????????????System.out.println(person.getAge());??
  32. ????????}?catch?(FileNotFoundException?e)?{??
  33. ????????????e.printStackTrace();??
  34. ????????}??
  35. ????}??
  36. }??


输出结果:

?

?

[javascript]?view plaincopy ?
  1. {"Person":{"name":"geniushehe","age":16}} ?
上一篇: 与国内某知名互联网公司交流后的心得 下一篇: 没有下一篇了!
发表评论
用户名: 匿名