Java serialization, deserialization example

Source: Internet
Author: User
Tags serialization
Package work;
Import Java.io.ByteArrayInputStream;
Import Java.io.ByteArrayOutputStream;
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.io.Serializable; public class Test {public static void main (string[] args) throws FileNotFoundException, IOException, CLASSNOTFOUNDEXCEP
		tion {//TODO auto-generated method Stub box a=new box (); Serialized as a file that can be used to save ObjectOutputStream oos1=new ObjectOutputStream (New FileOutputStream ("c:\\users\\wind\\desktop\\
		A.txt "));
		Oos1.writeobject (a);
		ObjectInputStream ois=new ObjectInputStream (New FileInputStream ("C:\\users\\wind\\desktop\\a.txt"));
		Box b= (Box) ois.readobject ();
		
		System.out.println (B.gettiji ()); 
		Serialized as a string that can be used for network transport bytearrayoutputstream Byteos = new Bytearrayoutputstream ();
		ObjectOutputStream oos2=new ObjectOutputStream (Byteos);
		Oos2.writeobject (a); StringResualtstring=byteos.tostring ("iso-8859-1");//Character set cannot be modified ObjectInputStream ois2=new ObjectInputStream (new
		Bytearrayinputstream (Resualtstring.getbytes ("iso-8859-1"));
		Box c= (Box) ois2.readobject ();
	System.out.println (C.gettiji ());
	The class box implements SERIALIZABLE{//declaration interface, without implementing the concrete method int i,j,k;
		Box () {i=1;
		j=2;
	k=3;
	int Gettiji () {return i*j*k;
 }
}

Related Article

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.