Share java Serializable Functions

Source: Internet
Author: User

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



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.