标签:序列化 java xml xstream serialization
1.序列化到txt文件中
//Person.java
package com.huowolf.api; import java.io.Serializable; /* * 把对象转换为字节序列的过程称为对象的序列化。 * 把字节序列恢复为对象的过程称为对象的反序列化。 * * 任何类型只要实现了Serializable接口,就可以被保存到文件中, * 或者作为数据流通过网络发送到别的地方。 * 也可以用管道来传输到系统的其他程序中。 * * 想要实现序列化的类都应该实现该接口 * transient:非静态数据不想被序列化可以使用这个关键字修饰。 */ public class Person implements Serializable{ private static final long serialVersionUID = 952L; private String userName; private String password; private int age; public Person(String userName, String password, int age) { super(); this.userName = userName; this.password = password; this.age = age; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
package com.huowolf.api; /* * 需求:在D盘创建文件“user.txt"应用对象序列化知识将用户信息保存到文件中, * 之后将对象读入修改密码后在写回文件(用户包括用户名,密码,年纪3个属性) */ 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.Scanner; public class SerializationDemo { public static void readObj() throws Exception { ObjectInputStream ois = new ObjectInputStream(new FileInputStream("d:\\user.txt")); Person p = (Person) ois.readObject(); System.out.println("用户名:"+p.getUserName()+" 密码:"+p.getPassword()+" 年龄:"+p.getAge()); ois.close(); } public static void writeObj(Person p) throws FileNotFoundException, IOException { ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("d:\\user.txt")); oos.writeObject(p); oos.close(); } public static void main(String[] args) throws Exception { Scanner sc = new Scanner(System.in); Person p = new Person("小红","111111",18); writeObj(p); readObj(); System.out.println("请输入修改的密码:"); String word = sc.nextLine(); sc.close(); p.setPassword(word); System.out.println(p.getUserName()+" "+p.getPassword()+" "+p.getAge()); writeObj(p); } }
//SerializeXmlDemo .java
package com.huowolf.api; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import com.thoughtworks.xstream.XStream; /* * 序列化、反序列化为XML文件,使用了XStream来序列化, * 需要引入xstream-1.4.7.jar包的支持, * http://xstream.codehaus.org/download.html 处可以下载jar, * 然后引入到Eclipse中的build path中。 */ public class SerializeXmlDemo { //序列化到xml文档 public static void serializeToXml() throws IOException { Person[] myPerson = new Person[2]; myPerson[0] = new Person("Jay","1111",24); myPerson[1] = new Person("Tom","2222",23); XStream xstream = new XStream(); FileOutputStream fos = new FileOutputStream("myPerson.xml"); xstream.toXML(myPerson,fos); System.out.println(xstream.toXML(myPerson)); } //反序列化xml文档 public static void deSerializeFromXm() throws IOException { XStream xs = new XStream(); Person[] myPerson = null; FileInputStream fis = new FileInputStream("myPerson.xml"); myPerson=(Person[])xs.fromXML(fis); if (myPerson != null) { for (int i=0;i<myPerson.length;i++) { System.out.println("用户名:"+myPerson[i].getUserName()+" 密码:"+myPerson[i].getPassword()+" 年龄:"+myPerson[i].getAge()); } } } public static void main(String[] args) throws Exception { serializeToXml(); deSerializeFromXm(); } }
<com.huowolf.api.Person-array> <com.huowolf.api.Person> <userName>Jay</userName> <password>1111</password> <age>24</age> </com.huowolf.api.Person> <com.huowolf.api.Person> <userName>Tom</userName> <password>2222</password> <age>23</age> </com.huowolf.api.Person> </com.huowolf.api.Person-array> 用户名:Jay 密码:1111 年龄:24 用户名:Tom 密码:2222 年龄:23
加油!每一天!
标签:序列化 java xml xstream serialization
原文地址:http://blog.csdn.net/huolang_vip/article/details/41391959