[Fragmented] Java Study Notes-Java object serialization and file IO Processing

Source: Internet
Author: User

1. Object serialization is to save the current state of the object.

2.There are two main ways to save the object status: 

1)Object serialization (write the object to a file and deserialize it later, as shown in the following example)

If the stored data is used in JavaProgramObject serialization is used for internal interaction;

2)Write a file (write an object to a text file. Note thatText Files)

If the stored data is used to interact with other non-Java programs, it is saved to a text file.

3.If the object needs to be serialized, its class must be implemented.SerializableInterface.

If a class is serializable, its subclass can be automatically serialized.

4. Object serialization steps:

1) create a file output stream;

2) create an object output stream;

3) Write object status;

4) Close the object output stream;

 
// 1. create File output stream fileoutputstream FS = new fileoutputstream ("outputfile. file "); // 2. create an object output stream objectoutputstream OS = new objectoutputstream (FS); // 3. write object status OS. writeobject (characterone); OS. writeobject (charactertwo); OS. writeobject (characterthree); // 4. disable the output stream OS of the object. close ();

The order in which objects are written to files is:

Where,Objectoutputstream converts an object to word-based throttling, and fileoutputstream writes a byte stream to a file.

5. The serialized object stores all the attributes of the object, and the object referenced by the instance variable of the object is also serialized.

If an instance variable cannot or should not be serialized, mark itTransient. Mark attributeTransientWhen the object is serialized, the property is savedNullValue.

6. Object deserialization steps:

1) create a file input stream;

2) create an object input stream;

3) read the object status;

4) convert the object type (because the object is of the object type after reading the object );

5) Close the object input stream;

// 1. create a file input stream. If the file does not exist, the program throws an exception fileinputstream FS = new fileinputstream ("outputfile. file "); // 2. create object input stream objectinputstream OI = new objectinputstream (FS); // 3. read object state object one = Oi. readobject (); object two = Oi. readobject (); object three = Oi. readobject (); // 4. conversion object type gamecharacter elf = (gamecharacter) One; gamecharacter troll = (gamecharacter) Two; gamecharacter magician = (gamecharacter) Three; // 5. closes the object input stream Oi. close ();

The order in which the object is written is:

 

After the object reads the object input stream, JVM will try to find and load the class of the object. If it fails, an exception is reported.

7.When the object is restored, the static variables maintain the original appearance of the class, instead of the storage.

8. Steps for writing object status to a file: (use bufferedwriter)

 
// First store data in the buffer, and then link to the file bufferedwriter writer = new bufferedwriter (New filewriter (afile); // write data writer. write ("blabla"); // close writer. close ();

The order in which objects are written to files:

 

Reason for using the buffer Writer: If filewriter is used directly, it takes many times to write to the hard disk, which is slow (the hard disk read/write speed is far slower than the memory read/write speed ). With the buffer writer, you can buffer the data first and then write it to the hard disk file at one time.

9. Steps for reading objects from a file: (use bufferedreader)

 
// Filereader is the stream of characters connected to text files filereader = new filereader (new file ("file.txt"); // use bufferedreader to link to filereader, make receiving more efficient bufferedreader reader = new bufferedreader (filereader); string line = NULL; // print the data while (line = reader. readline ())! = NULL) {system. Out. println (line );}

During File Reading, bufferedreader is used to link filereader, making receiving more efficient.

---------------------------------------

The following is an example of Object serialization, deserialization, and file IO.

Class cashcheck implements serializable {Private Static final long serialversionuid =-bytes; private string cashcheckid; private int cashamout; Public cashcheck () {} Public String getcashcheckid () {return cashcheckid ;} public void setcashcheckid (string cashcheckid) {This. cashcheckid = cashcheckid;} public int getcashamout () {return cashamout;} public void setcashamout (INT cashamout) {This. casham Out = cashamout;} public class serializedemo01 {public static void main (string [] ARGs) {cashcheck check = new cashcheck (); check. setcashamout (10000); check. setcashcheckid ("notifier");/** serialize object */try {// 1. creates a file output stream and writes bytes to the file fileoutputstream FS = new fileoutputstream (new file ("C: \ Documents and Settings \ Administrator \ Desktop \ notifier. file "); // 2. creates an object output stream and converts the object into a byte objectoutputstream OS = new objectoutputstr EAM (FS); // 3. write object OS. writeobject (check); // 4. disable the output stream OS of the object. close ();} catch (filenotfoundexception e) {e. printstacktrace ();} catch (ioexception e) {e. printstacktrace ();}/** deserialization object */try {// 1. create a file input stream to convert the file into a byte fileinputstream Fi = new fileinputstream ("C: \ Documents and Settings \ Administrator \ Desktop \ notifier. file "); // 2. creates an object input stream and writes bytes to the object objectinputstream OI = new objectinputstream (FI); // 3. read object O1 = Oi. Readobject (); If (O1 instanceof cashcheck) {cashcheck check1 = (cashcheck) O1; system. out. println (check1.getcashcheckid (); system. out. println (check1.getcashamout ();} // 4. closes the object input stream Oi. close ();} catch (filenotfoundexception e) {e. printstacktrace ();} catch (ioexception e) {e. printstacktrace ();} catch (classnotfoundexception e) {e. printstacktrace ();}/** read the serialized file as a file. It is garbled. */try {filereader Fr = new filereader (New file ("C: \ Documents and Settings \ Administrator \ Desktop \ notifier. file "); bufferedreader BR = new bufferedreader (FR); string line = NULL; while (line = BR. readline ())! = NULL) {system. out. println (line) ;}} catch (filenotfoundexception e) {e. printstacktrace ();} catch (ioexception e) {e. printstacktrace ();}}}

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.