Java Object Flow

Source: Internet
Author: User
Tags object serialization

Object Flow

The ObjectInputStream ObjectOutputStream class is a subclass of InputStream and OutputStream, respectively, and the object output stream uses the WriteObject (object obj) method, Writes an object, obj, to a file, using ReadObject () to read an object.

Construction Method:

ObjectInputStream (InputStream in)

ObjectOutputStream (OutputStream out)

When you are ready to write an object to a file, first create an output stream with the OutputStream subclass.

OutputStream OutStream = new FileOutputStream ("file_name");

ObjectOutputStream objstream = new ObjectOutputStream (OutStream);

When using object flow to write or read objects, it is necessary to ensure that the object is serialized, so that the object can be correctly written to the file, and the object can be correctly read back to the program.

The so-called object serialization is an implementation means of converting an object into a binary data stream, and the object can be transmitted and saved conveniently by serializing the object. The two classes of Obejctinputstream and ObjectOutputStream are provided in Java for the operation of serializing an object. The input-output stream class for storing and reading objects requires the implementation of the serializable interface to implement the serialization of the object, but no method is defined in the serializable interface and is used only as a token for special processing by the compiler.

 1 package project; 2 Import java.io.*; 3//Implement Serializable Interface 4 class TV implements serializable{5/** 6 * 7 */8 private static final long SE Rialversionuid = 1597309922188243103L; 9 string name;10 int price;11 public void SetName (string name) {this.name = name;13}14 Publ IC void Setprice (int price) {this.price = price;16}17 public String getName () {return name;1 9}20 public int GetPrice () {return price;22}23}25 public class Main{26 public static void Main (string[] args) {TV Changhongtv = new TV (); Changhongtv.setname ("Changhong") Changhong Tv.setprice (5678); file File = new file ("Changhong"); try {32//serialize write to file Outp Utstream outputstream = new FileOutputStream (file), ObjectOutputStream objectoutputstream = new ObjectOutput Stream (OutputStream); Objectoutputstream.writeobject (CHANGHONGTV); Objectoutputstream.close (); 37 38//Serialized Read Object InputStream I Nputstream = new FileInputStream (file), ObjectInputStream ObjectInputStream = new ObjectInputStream (inputStr             EAM); Xinfeitv TV = (TV) objectinputstream.readobject (); Objectinputstream.close (); 43             Xinfeitv.setname ("New Flying TV"); Xinfeitv.setprice (6666); 47 48 System.out.println ("Changhong name" +changhongtv.getname ()); System.out.println ("Changhong Price" +chan Ghongtv.getprice ()); System.out.println ("Xinfei name" +xinfeitv.getname ());            Tem.out.println ("Xinfei Price" +xinfeitv.getprice ()); ClassNotFoundException} catch (e) {todo:handle exception56 syste M.OUT.PRINTLN ("Cannot read object");}catch (IOException e) {//TODO: Handle exception59 System.out.println (e); 60}61}62} 

The realization of the object's serialization facilitates the cloning of the object, so that the change of the duplicate entity will not cause the change of the original object entity.

Here is an example:

In addition, the Componet class in the java.awt package is the class that implements the Serializable interface, so the program can implement writing the component to the output stream and then read the clone of the component with the input stream.

Java Object Flow

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.