You can serialize a java object to a file, or deserialize a file to a java object.
Originality is not easy. For details, refer to the Source: Share the java Serializable function.
DeserializeFile2Object.java
Package com. zuidaima. util; import java. io. fileInputStream; import java. io. objectInputStream;/*** deserialize the file to a java object ** @ author javaniu **/public class DeserializeFile2Object {public static void main (String [] args) {Address address = deserialzeAddress (); System. out. println (address);} public static Address deserialzeAddress () {Address address; try {FileInputStream fin = new FileInputStream ("c:/address. ser "); ObjectInputStream ois = new ObjectInputStream (fin); address = (Address) ois. readObject (); ois. close (); return address;} catch (Exception ex) {ex. printStackTrace (); return null ;}}}
SerializeObject2File.java
Package com. zuidaima. util; import java. io. fileOutputStream; import java. io. objectOutputStream;/*** serialize a java object to a file ** @ author javaniu **/public class SerializeObject2File {public static void main (String args []) {serializeAddress ("Chaoyang District, Beijing", "China");} public static void serializeAddress (String street, String country) {Address address = new Address (); address. setStreet (street); address. setCountry (country); try {FileOutputStream fout = new FileOutputStream ("c: \ address. ser "); ObjectOutputStream oos = new ObjectOutputStream (fout); oos. writeObject (address); oos. close (); System. out. println ("Done");} catch (Exception ex) {ex. printStackTrace ();}}}
Code: http://www.zuidaima.com/share/1550463266802688.htm