The understanding of serialization in J2SE _jsp programming

Source: Internet
Author: User
Tags serialization
Java is the embodiment of a simple programming style, serialization as one of the most common functions, in Java design is particularly "simple." With the help of ObjectInputStream and ObjectOutputStream, we can achieve serialization easily.

As long as our class By implementing the Java.io.Serializable interface, we can serialize an object by using the ObjectOutputStream writeobject () method, and using the ObjectInputStream ReadObject () method, You can return an object that is read out. The serializable interface does not require us to implement any methods.

Here is an example that gives us a sense of sensibility:

Serial implements the Java.io.Serializable interface, which is a class that needs to be serialized. We first construct a serial object Serial1 then save (serialize) it in a file, read it out (deserialize), and print its contents.

Package Stream;

/**

* @author Favo Yang

*/

Import java.io.*;

public class serial implements Serializable {
int company_id;
String company_addr;
Boolean Company_flag;
Public serial () {}//is different from C + +, and can
Public serial (int company_id,string Company_addr,boolean company_flag) {
this.company_id=company_id;
THIS.COMPANY_ADDR=COMPANY_ADDR;
This.company_flag=company_flag;
}

public static void Main (string[] args) {
Serial serial1 = new serial (752, "Dayer Street #5 building 02-287", false);//Construct a new object
FileInputStream In=null;
FileOutputStream Out=null;
ObjectInputStream Oin=null;
ObjectOutputStream Oout=null;

try {
out = new FileOutputStream ("5.txt");
Oout = new ObjectOutputStream (out);
Serial1.serialize (oout);//serialization
Oout.close ();
Oout=null;
in = new FileInputStream ("5.txt");
oin = new ObjectInputStream (in);
Serial Serial2 = Serial.deserialize (oin);//deserialization
System.out.println (SERIAL2);//Print result
catch (Exception ex) {
Ex.printstacktrace ();
} finally{
try {
if (in!= null) {
In.close ();
}
if (oin!= null) {
Oin.close ();
}
if (out!= null) {
Out.close ();
}
if (oout!= null) {
Oout.close ();
}
catch (IOException Ex1) {
Ex1.printstacktrace ();
}
}
}

/**
* Deserialize
*/

public static serial Deserialize (ObjectInputStream oin) throws exception{
Serial s= (serial) oin.readobject ();
return s;
}

Public String toString () {
Return "DATA:" "+company_id+" "+company_addr+" "+company_flag;
}

/**
* Serialize
*/

public void serialize (ObjectOutputStream oout) throws exception{
Oout.writeobject (this);
}
}

Run Result:

data:752 Dayer Street #5 building 02-287 False
  
The results are printed correctly.

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.