Java-stream end and Exception Handling Mechanism, java-Exception Handling

Source: Internet
Author: User
Tags object serialization

Java-stream end and Exception Handling Mechanism, java-Exception Handling

1.1java.io.objectInputStream object input stream: used to read and convert a group of bytes (a group of bytes converted by writing an object through the object output stream) to the corresponding object. The object output stream converts an object into a group of bytes when writing it out. It is called the object serialization object input stream that reads and restores this group of bytes. It is called object deserialization.
1.2java.io.Serializable Serializable serialization Interface
When a class implements the Serializable interface, you should add a constant to the current class: serialVersionUID, the serialization version number.
If the serialization version number is not specified, the compiler will add one in the compiled class file by default, and the value is generated based on the current class structure, but this has a problem, if the structure of the current class changes, the version number changes. Therefore, we recommend that you set the version number to affect the deserialization of the object: when an object input stream reads an object and tries deserialization, it checks whether the version number of the object is consistent with the current version number of the class. if the version number is consistent, deserialization is successful, if the version number of the deserialization failed is the same, if the object to be deserialized is inconsistent with the existing structure of the current class, the compatibility mode is adopted, that is: if the object belongs to an existing class, it is restored, and if it does not, it is ignored.
Transient -- Keyword: This keyword is used to modify attributes. When the object is serialized, the value of this attribute will be ignored, so that the object can be "reduced ".
1.3java.io.FileOutputStream; Java is divided into byte streams based on the unit of stream read/write data. byte streams read/write data in bytes. Character streams read/write data in unicode units, however, the underlying layer still reads and writes bytes, but the conversion of bytes and characters involves the completion of the upstreaming Weiter and Reader, which are the parent classes of all the upstreaming streams. They are a pair of abstract classes, specifies the read/write character methods required for all the upstream streams. Conversion stream OutoutStreamWriter and InputStreamReader are a pair of common implementation classes for a compaction stream.
The OutputStreamWriter constructor supports the second parameter. You can specify a character set. In this way, all characters written in the stream are converted according to the specified character set. If the second parameter is not specified, the default Character Set conversion is installed.
1.4java.io.PrintWriter; buffer character stream: buffer the built-in buffer of the character stream to improve the efficiency of reading and writing characters, and buffer the characteristics of the character stream to read and write strings by row.
Java. io. BufferedWriter
Java. io. BufferefReader
Java. io. printWriter is a commonly used buffer character output stream, and also provides the row refresh function of the Buddha. Because it is always nested with BufferedWriter during PrintWriter creation, the actual buffer operation is implemented by BufferefWriter.
PrintWriter (pw) supports the construction of file operations directly.
Printwriter (String path, String snc)
Printwriter (File file, String snc)
Csn: charset name Character Set name
The construction method of PrintWriter in the stream link:
PrintWriter (OutputStream out)
PrintWriter (Writer out)
Using the appeal constructor method, there is a corresponding overload constructor that requires the second parameter to be passed in. This parameter is a boolean value. If this value is true, pw has the automatic row refresh function, that is: PW is used every time. Println () is automatically written
Flush operation
PrintWriter (OutputStream out, boolean autoflush)
PrintWriter (Writer out, boolean autoflush)
1.5java.io.BufferedReader; the buffer byte input stream can read strings by row
BufferedReader provides the following methods:
String readLine () reads several consecutive characters until the linefeed is read. All the previous characters are returned as one String. Note: The returned string does not contain the last line break. If the returned string is null, it indicates the end.
2.1 try-catch in the exception capture mechanism
Catch can be defined multiple times. When there are different handling methods for exceptions that may occur in the try code block, you need to capture these exceptions separately and write the code to solve them, but you should develop a habit, it is to capture an Exceotion at the end, which will avoid program interruption caused by an uncaptured exception. When there is an inheritance relationship between the caught exceptions, you need to capture the subclass exception definition above, and then define the parent type exception.
In the exception capture mechanism, finally; finally blocks can only be defined at the end of the exception capture mechanism, that is, after a try or after a catch. The finally block ensures that the code in the finally block will be executed no matter whether or not the code in the try statement block throws an exception. Therefore, the code to be executed for unrelated exceptions is usually put into the finally block to ensure the execution, for example, closing the stream operation in the IO operation.

Note: finalize. Before an object is to be released by GC, GC calls the finalize method of the object. After the object is called, it is released. The finalize method is the Object-defined method.

 

I am a beginner. If any updates are poor, I would like to thank you!

More highlights will be updated later!

 

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.