1. Serializing to TXT file
Person.java
Package Com.huowolf.api;import java.io.serializable;/* * The process of converting an object to a sequence of bytes is called serialization of an object. * The process of reverting a sequence of bytes to an object is called deserialization of the object. * * Any type can be saved to a file as long as the serializable interface is implemented, or sent to another location as a data stream over the network. * Pipelines can also be used to transfer to other programs in the system. * * The class that wants to implement serialization should implement the interface * Transient: non-static data does not want to be serialized can be modified using this keyword. */public class Person implements serializable{private static final long serialversionuid = 952l;private String Userna Me;private string password;private int age;public person (string userName, String password, Int. age) {super (); This.usernam E = 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.) {this.age = age;}}
Serializationdemo.java
Package com.huowolf.api;/* * Requirements: Create file "User.txt" in D drive Application object serialization knowledge to save the user information to a file, * After the object is read into the modified password after the file is written back (user includes user name, password, age 3 attributes) */ 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 Objectinputstr EAM (New FileInputStream ("D:\\user.txt")); Person P = (person) ois.readobject (); System.out.println ("Username:" +p.getusername () + "Password:" +p.getpassword () + "Age:" +p.getage ()); Ois.close ();} public static void Writeobj (person p) throws FileNotFoundException, IOException {objectoutputstream oos = new OBJECTOUTPU TStream (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 ("Little Red", "111111", Writeobj (p); readobj (); System.out.priNtln ("Please enter the modified password:"); String Word = Sc.nextline (); Sc.close ();p. SetPassword (word); System.out.println (P.getusername () + "" "+p.getpassword () +" "+p.getage ()); Writeobj (P);}}
2. Serializing to an XML file
Serializexmldemo. java
Package Com.huowolf.api;import Java.io.fileinputstream;import Java.io.fileoutputstream;import java.io.IOException; Import com.thoughtworks.xstream.xstream;/* * serialized, deserialized into XML file, serialized using XStream, * need to introduce Xstream-1.4.7.jar package support, */HTTP xstream.codehaus.org/download.html can download the jar, * and then introduce it into the build path in eclipse. */public class Serializexmldemo {//serialization to XML document public static void Serializetoxml () throws IOException {person[] MyPerson = NE W 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)); }//deserialization XML document 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 ("Username:") +myperson[i].getusername () + "Password:" +myperson[i].getpassword () + "Age:" +myperson[i].getage ()); }}}public static void Main (string[] args) throws Exception {serializetoxml ();d eserializefromxm ();}}
This program outputs the result:
<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> User name: Jay Password: 1111 Age: 24 User name: Tom Password: 2222 Age: 23
Come on! Every day!
Serialization and deserialization of Java