End of use of java streams and details of the exception handling mechanism of

  • 2020-10-07 18:38:26
  • OfStack

1.1 java. io. objectInputStream object input stream: Used to read and convert a set of bytes (a set of bytes converted by writing an object through the object output stream) to the corresponding object. Object output stream The process of converting an object to a set of bytes when it is written out is called object Serialization.Object Input stream The process of reading and restoring the set of bytes to an object is called object deserialization

1.2 java. io. Serializable Serializable serialization interface

When a class implements the Serializable interface, you should add a constant to the current class: serialized version number serialVersionUID

Serialization version number if not specified, then the compiler will in the compiled class add a file by default, the value is generated according to the current class structure, but it has a problem, if the current class structure has changed, so the version number change, so suggest make the version number on its own version number to influence the outcome of object deserialization: When object input stream to read 1 will detect object and to deserialize the object version number and the existing version number is 1, 1 to deserialize success, not 1 to deserialize the version number 1 cause of failure cases, if to be deserialized object with the current class of existing structure is one cause is that the compatibility mode, namely: the object of the animal sign an existing class still others reduction, not be ignored.

transient-- Keyword: this keyword modifies the property so that when the object is serialized, the value of the property is ignored, thus slimming the object

1.3 java. io. FileOutputStream; Java according to the unit of data flow, speaking, reading and writing are divided into: byte streams, character stream byte stream in bytes read and write data flow characters in character (unicode) as unit to read and write data, but the underlying essence, speaking, reading and writing bytes, just byte characters with the character conversion work flow Weiter and Reader is the flow of all characters of the parent class, they are 1 to abstract class, specifies all the characters of the flow to have the method of reading and writing characters. Conversion streams OutoutStreamWriter and InputStreamReader are one pair of common implementation classes for character streams

The OutputStreamWriter constructor supports a second parameter, which specifies the character set so that characters written through the stream will be converted to the specified character set, and if the second parameter is not specified, the system default character set conversion is installed

1.4 java. io. PrintWriter; Buffered character stream: The buffered character stream has a built-in buffer to improve the efficiency of reading and writing characters, and the buffered character stream features the ability to read and write strings on a line-by-line basis.

java.io.BufferedWriter

java.io.BufferefReader

java.io.printWriter is a commonly used buffered character output stream, and also provides a line refresh function for the supporting character. Since PrintWriter is always nested inside BufferedWriter when it is created, the actual buffering operation is implemented by BufferefWriter

printWriter (pw) supports constructors that operate directly on files

printwriter (String path,String snc)

printwriter (File file,String snc)

csn:charset name character set name

PrintWriter constructors in stream links:

PrintWriter(OutputStream out)

PrintWriter(Writer out)

Using the appellate constructor, there is a corresponding overloaded constructor that requires the passing of a second parameter, which is the value boolean. If the value is true, pw has an automatic line refresh function, that is: whenever PW is used. println() does this automatically when it writes out the content

flush operation

PrintWriter(OutputStream out, boolean autoflush)

PrintWriter(Writer out, boolean autoflush)

1.5 java. io. BufferedReader; The buffered byte input stream can read strings on a row basis

BufferedReader provides methods:

String readLine() reads several characters in a row and returns all previous characters as a string until a line break is read. Note that the returned string does not contain the last line break, and a return of null indicates the end.

2.1 try-ES125en in the exception capture mechanism

catch can define multiple for try exceptions that may occur in a code block has a different approach, the need to capture these anomalies and solution code alone but should be a habit, is in the final capture a Exceotion, this will avoid caused by throwing an uncaught exception 1 program interruptions, when capture inheritance relationships between abnormal, need to subclass exception definitions above capture, after defining the parent type.

finally in the exception capture mechanism; finally blocks can only be defined at the end of the exception catching mechanism, after try or after 1 catch. The finally block guarantees that the code in the try block will execute whether or not the code in the try block throws an exception. So it's common to put code that executes unrelated exceptions into an finally block to ensure execution, such as a close flow operation in an IO operation.

Note: finalize; When an object is about to be released by GC, GC calls the object's finalize method, and the object is released after the call. The finalize method is the method defined by Object

I am a beginner, if have update bad, welcome this big god point out, thank everybody!


Related articles: