[Java] Java serialization saves the content of an object to a file and reads the object from the file

Source: Internet
Author: User

[Java] Java serialization saves the content of an object to a file and reads the object from the file

In Java, you can save the content of an object to a file and read the object from the file. When you look at the KEA code, you will find that it does this: public class keamodelbuilder implements optionhandler {/** the KEA filter object */keafilter M_keafilter= NULL; // The keafilter object is a member variable of the keamodelbuilder class.

// When running a Java program, if we need to save all the content of an object to a file, we can do this:

/*** Saves the extraction model to the file. */Public void savemodel () throws exception {writable bufferedout = new bufferedoutputstream (New fileoutputstream (m_modelname); // file path objectoutputstream out = new objectoutputstream (bufferedout); out. writeobject (m_keafilter); out. flush (); out. close ();} // load information from the file to the object. You can do this:/*** loads the extraction model from the file. */Public void loadmodel () throws exception {bufferedinputstream instream = new bufferedinputstream (New fileinputstream (m_modelname); // file path objectinputstream in = new objectinputstream (instream ); m_keafilter = (keafilter) in. readobject (); In. close () ;}}in this way, when the savemodel () method of keamodelbuilder is called, all information of the object m_keafilter can be written to the file. you can call loadmodel () to read object information from a file. then the m_keafilter object can be used normally.
This method is very convenient. It is worth learning.

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.