Java ObjectInputStream and Objectoutputstream__java

Source: Internet
Author: User
Java ObjectInputStream and ObjectOutputStream Overview

The stream can serialize a basic data and object, or read a basic data and object into the program. Which is to perform the serialization and deserialization operation

The class of the serialized object must implement an example of Serializable interface serialization and deserialization of an object

ObjectOutputStream output = null;

ObjectInputStream input = null;
    try {//Create two object product P1 = new Product ("fridge", 1000);

    Product P2 = new Product ("TV", 2000);
    Create object Output stream = new ObjectOutputStream (New FileOutputStream ("Product.obj"));
    Write two objects output.writeobject (p1);

    Output.writeobject (p2);

    Turn off the output stream output.close ();

    Creates an object input stream, reads the persisted object input = new ObjectInputStream (New FileInputStream ("Product.obj"));
    Print object Information System.out.println (Input.readobject ());

    System.out.println (Input.readobject ());

Close flow input.close (); catch (IOException e) {e.printstacktrace ();} finally {try {if (input!= null) {Input.clo
        SE ();
    } catch (IOException e) {e.printstacktrace ();
            }finally {try {if (output!= null) {output.close ();
        } catch (IOException e) {e.printstacktrace (); }
    }
}
Run results

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.