Object Flow ObjectInputStream and ObjectOutputStream in Javaio

Source: Internet
Author: User

ObjectInputStream and ObjectOutputStream can write objects on the hard disk or read them from the hard disk, and of course they can be transmitted over the network. But the manipulated object must be serialized.

What is the serialization of an object? The goal of object serialization is to save the object on disk, or to allow the object to be transferred across the network.

The API describes the following

The description in the API is as follows

An object that implements serialization package Cn.bean.demo.io.object;import java.io.serializable;//If this object has an instance property "and the object of this instance is a custom object, He must also be serialized, otherwise the person will not be successfully written to disk and can self-authenticate the "public class person implements serializable{/** * Object serialization version number */private static final Long Serialversionuid = 8761043201815925119l;private string Name;private string sex;private string Age;public string GetN Ame () {return name;} public void SetName (String name) {this.name = name;} Public String Getsex () {return sex;} public void Setsex (String sex) {this.sex = sex;} Public String Getage () {return age;} public void Setage (String age) {this.age = age;} Public person (string name, String sex, String age) {super (); this.name = Name;this.sex = Sex;this.age = age;} @Overridepublic String toString () {return "person [name=" + name + ", sex=" + Sex + ", age=" + Age + "]";}}

  

Ways to read and write objects package Cn.bean.demo.io.object;import Java.io.fileinputstream;import Java.io.filenotfoundexception;import Java.io.fileoutputstream;import Java.io.ioexception;import Java.io.inputstream;import Java.io.ObjectInputStream; Import Java.io.objectoutputstream;import Java.io.outputstream;public class Objectoutandinputstreamtest {public static void Main (string[] args) {//testobjectoutputstream (); Testobjectinputstream ();} /** * Writes an object to a disk file */static void Testobjectoutputstream () {//try (OutputStream out = new FileOutputStream ("Object.txt"); Ob Jectoutputstream ObjectOutputStream = new ObjectOutputStream (out);) The {///write object must implement the serialization of person man = new Person ("Nigulas", "Male", "a"); Objectoutputstream.writeobject (person);} catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace ();}} /** * Read object information from disk file */static void Testobjectinputstream () {try (InputStream in = new FileInputStream ("Object.txt"); objecti Nputstream ObjectInputStream = new ObjectInputStream (in);) {person person= (person) oBjectinputstream.readobject (); SYSTEM.OUT.PRINTLN (person);} catch (Exception e) {//TODO auto-generated catch Blocke.printstacktrace ();}}}

  

Object Flow ObjectInputStream and ObjectOutputStream in Javaio

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.