JAVAI/O System 2

Source: Internet
Author: User
Tags object serialization

Http://www.cnblogs.com/hellokitty1/p/4473952.html

Data flow:

Datainputstream,dataoutputstream. Basic data types and string objects that can be used to read and write Java in a computer-independent format

Object Flow:

Objectinputstream,objectoutputstream.

Serialization: Holds the "Panorama" of an object in memory.

In order to implement object serialization, the corresponding class must implement the Serializable interface.

There is no method defined in the serializable interface, called the "tag interface."

If a transient modifier is added, the value of the property will not be serialized.

Deserialization: The process of reverting a sequence of bytes to an object using the ObjectInputStream class.

  1 package com.lovo.day2;  2 3 Import java.io.IOException;  4 Import java.io.Serializable;  5 Import Java.util.Date; 6 7 public class Student implements Serializable {8 9 private static final long Serialversionuid = 4282130869653 374904L; Ten private String name; the private int age; Private Boolean sex; + Private double money; The private Date birth; -Private Course Course;         Public Student (String name, int age, Boolean sex, double money, Date birth, Course Course) {20 Super (); THIS.name = name; This.age = age; This.sex = sex; This.money = money; This.birth = birth; This.course = course;  The public string GetName () {"Return name", "SetName"), and the public void (string name) {this.name = name; n-Notoginseng public int getage () {the "return age; c void Setage (int Age) {"This.age = age", and "Issex") {47} 48 49 public void Setsex (Boolean sex) {this.sex = sex; n-A-si-public double-Getmoney () {si-RE Turn money; Setmoney (double money) {$ this.money = money; a.) Tbirth () {This.birth return birth; Setbirth (Date birth) {n = birth;  The Course GetCourse () {Course return Course; the setcourse of the Course) {this.course = course; n} @Override the public String toString () {RET  Urn "Student [name=" + name + ", age=" + Age + ", sex=" + Sex + ", money=" + Money + ", birth=" + Birth " + ", course=" Bayi + course + "]"; ReadObject (Java.io.ObjectInputStream in) throws IOexception, ClassNotFoundException {System.out.println ("read"); this.name = In.readutf ( ); This.age = In.readint (); This.sex = In.readboolean (); This.course = (course) in.readobject (); This.money = In.readdouble (); 94 private void WriteObject (Java.io.ObjectOutputStream out) throws IOException {SYSTEM.OUT.PR Intln ("write"); Out.writeutf (this.name); Out.writeint (This.age); 98 Out.writeboolean (this.sex); Out.writeobject (course); out.writedouble (This.money); 101}102 */103}
1 package com.lovo.day2; 2  3 import java.io.Serializable; 4  5 public class Course implements Serializable {6  7     private static FINA L Long serialversionuid = 1L; 8      9     private int code;10     private String name;11 public     int GetCode () {         return code;14     }15 Public     void Setcode (int code) {         This.code = code;18     }19     -Public String getName () {         Return name;22     }23 public     void SetName (String name) {         this.name = name;26     }27     Course (int code, String name) {very         super ();         this.code = code;31         this.name = name;32     }33     @ Override35 public     String toString () {         Course return "code=" + code + ", name=" + name + "]"; PNs     }38}
 1 package com.lovo.day2; 2 3 Import Java.io.FileInputStream; 4 Import java.io.FileNotFoundException; 5 Import Java.io.FileOutputStream; 6 Import java.io.IOException; 7 Import Java.io.ObjectInputStream; 8 Import Java.io.ObjectOutputStream; 9 Import java.util.date;10 One public class Objecttest {All public static void main (string[] args) {Studen T stu = new Student ("Zhang Fei", "true", 10.0, New Date (), New Course (15 1, "language"); Student stu2 = NE W Student ("Zhang Fei 2", +, True, 10.0, New Date (), New Course (17 2, "mathematics")); 18 19//serialization of the Object OutputStream out = null;21 try {% out = new ObjectOutputStream (New FILEOUTP Utstream ("D:\\student.bin")), Out.writeobject (Stu), Out.writeobject (STU2), and the catch ( FileNotFoundException e) {e.printstacktrace (); catch (IOException e) {E.printsta Cktrace (); FINAlly {if (out! = NULL) {$ try {out.close ();} CATC         H (IOException e) {e.printstacktrace (); 37}38}39}40 41 Deserialization ObjectInputStream in = null;43 the try {in = new ObjectInputStream (New FileInput Stream ("D:\\student.bin"); student stu1 = (student) in.readobject (); System.out.println (STU1)             ; System.out.println ("*****************"); System.out.println (Stu1.getcourse ()); 52 System.out.println ("#######################################"); Student Stu21 = (Student) in.readobject (); System.out.println (stu21); System.out.printl N ("*****************"); System.out.println (Stu21.getcourse ()); catch (Filenotfoun       Dexception e) {62      E.printstacktrace (); IOException catch (E) {e.printstacktrace ();                 Notfoundexception e) {e.printstacktrace (); 69} finally {(in! = NULL) try {in.close (); IOException} catch (e) {e.prints Tacktrace (); 73}74}75}76}77}

JAVAI/O System 2

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.