Serialization, deserialization, and IO serialization of Java-io stream objects

Source: Internet
Author: User

Serialization, deserialization, and IO serialization of Java-io stream objects

  • Basic serialization operations

1. Object serialization means to convert an Object into a byte sequence, and vice versa.

2. The ObjectOutputStream and writeObject methods are used to write objects to the output stream;

The deserialization stream (ObjectInputStream). The readObject method is used to read objects from the input stream.

3. Serializeable)

The object must implement the serialization interface before it can be serialized. Otherwise, an exception occurs. This interface does not have any method, but is a standard.

Package com. test. io; import java. io. fileInputStream; import java. io. fileOutputStream; import java. io. objectInputStream; import java. io. objectOutputStream; public class objectserialetest {/*** Object serialization * @ param file * @ throws Exception */public void ObjectOutput (String file) throws Exception {ObjectOutputStream oos = new ObjectOutputStream (new FileOutputStream (file); Student stu = new Student ("002", "Zhang Si", 12); oos. writeObject (stu); oos. flush (); oos. close ();}/*** deserialization of the object * @ param file * @ throws Exception */public void ObjectInput (String file) throws Exception {ObjectInputStream ois = new ObjectInputStream (new FileInputStream (file); Student stu = (Student) ois. readObject (); System. out. println (stu. toString (); ois. close ();} public static void main (String [] args) throws Exception {String file = "F: \ javaio \ obj. dat "; objectserialetest ost = new objectserialetest (); ost. objectOutput (file); ost. objectInput (file );}}

 

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.