The JDK framework analysis of the input-output class library in the--java.io package

Source: Internet
Author: User
Tags object serialization


Preface


Jdk,java Development Kit.

We must first realize that the JDK is just a Java base Class library, which is the base class library developed by Sun, and that's it, the JDK itself and the class library we write ourselves to summarize, technically, or at one level, that need to be compiled into bytecode, run in the JRE, The result of JDK compilation is jre/lib under the Rt.jar, we learn to use it to deepen the understanding of Java, improve our Java coding level.

All articles in this series are based on a JDK version of 1.7.16.


The content of this section


The Java IO Library provides a mechanism that can be called a link that connects one stream to another, forming a link to a flow pipeline. This mechanism is actually an application of decorative mode (Decorator).

Through the flow of links, you can dynamically increase the function of the stream, and this function is increased by the dynamic combination of some of the basic functions of the stream acquired.

We want to get an Io object, often to produce multiple IO objects, which is not very easy to grasp the Java IO Library, but this application of the decorator pattern in the IO Library provides us with the flexibility of implementation.


Classification of streams


Node stream: A stream class that reads or writes from a specific place, such as a disk or memory, such as a fileinputstream/fileoutputstream is a node stream.

Filter Flow: Use node streams as input and output, provide enhancements to functions, inherit from Filterinputstream/filteroututstream classes (such as DataInputStream provides the ability to read and write Java basic data types, Bufferedinputstream the ability to provide data caching).

Pipe flow: Used for communication between threads, such as Pipedinputstream/pipedoutputstream.


Demonstrates a feature-enhanced process:


FILE--"FileInputStream (reads bytes from file)--" bufferedinputstream (added buffering function)--"datainputstream (increased ability to read Java basic data types)--" data

Data--"DataInputStream (output stream written to Java basic type)--" Bufferedoutputstream (provides data write buffer function)--"FileOutputStream (write file)--"


Byte stream and character stream:


The Java language uses Unicode to represent characters.

Reader and writer are mainly used to read and write characters.


Serialization of objects


Converting an object to a stream of bytes is saved and later restored to this object, a mechanism called object serialization.

Saving an object on a permanent storage device is called persistence.

To be able to serialize an object, it must implement the Java.io.Serializable interface, which is a declarative interface, without any content, just to tell the compiler that objects can be serialized.

When an object is serialized, only non-static member variables of the object are preserved, and no member methods and static member variables can be saved.

If the member variable of an object is an object, the data members of the object are also saved.

If a serializable object contains a reference to a member variable of a non-serializable object, the entire serialization operation fails and throws an exception: Java.io.NotSerializableException. We can mark this reference as transient, and the object can be serialized as well. Because variables tagged with the transient keyword are discarded when serialized.


Serialversionuid


The serialization mechanism for Java is to validate version consistency by judging the serialversionuid of the class at run time.

(1) The object that implements the serializable interface can display the value of setting serialversionuid (that's what the JDK is doing in the source code)

(2) You can also not set the value of Serialversionuid, at this time, the Java serialization mechanism based on the compiled class (it through the class name, method name, and many other factors computed, theoretically is the relationship of one by one mappings, Which is the only one) automatically generates a serialversionuid as a serialized version of the comparison, in this case, if the class file (class name, method, etc.) does not change, add spaces, line breaks, comments are possible, compile multiple times, Serialversionuid does not change, and if you add methods, variables, and so on, you will report an exception: Java.io.InvalidClassException

(3) in daily production, we often set this: private static final long serialversionuid = 1L; At this point, the Java serialization mechanism will assume that the version is consistent, only the variables can be compared to the assignment, for the class and serialized data in the inconsistent places (such as serialization, after the class has been added to reduce the field), will be directly discarded


Object Serialization Code Demo


Import Java.io.fileinputstream;import Java.io.fileoutputstream;import Java.io.ioexception;import Java.io.objectinputstream;import Java.io.objectoutputstream;import Java.io.serializable;import Java.net.URI; Import Java.net.urisyntaxexception;public class Test {public static void main (string[] args) throws Exception {serial (); Person P = deserial (); System.out.println (P.getname ()); System.out.println (P.getage ());} private static void serial () throws Exception {person p = new person ("Zhangsan", 18); FileOutputStream fos = new FileOutputStream ("/users/puma/tt/person.txt"); ObjectOutputStream oos = new ObjectOutputStream (FOS); Oos.writeobject (P); Oos.flush (); Oos.close ();} private static Person deserial () throws Exception{fileinputstream fis = new FileInputStream ("/users/puma/tt/person.txt" ); ObjectInputStream ois = new ObjectInputStream (FIS); Person P = (person) ois.readobject (); return p;}} Class Parent implements Serializable {}class person extends Parent {private static final long serialversionuid = 1L;private string name;private Integer age;public person (String Name,integer age) {this.setname (name), This.setage (age); Public String GetName () {return name;} public void SetName (String name) {this.name = name;} Public Integer Getage () {return age;} public void Setage (Integer age) {this.age = age;}}


The JDK framework analysis of the input-output class library in the--java.io package

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.