The concept of IO streams
- IO is short for Input and Output.
- An IO stream is a stream of reading and writing data from one end to the other, hence the name “stream”.
The basic classification
- According to the basic unit of read and write data, it is divided into byte stream and character stream. Byte stream refers to a stream that reads and writes data in the unit of bytes. It can read and write files of any type. Character stream refers to a stream that reads and writes data in the unit of characters (2 bytes). Only text files can be read and written.
- According to the direction of reading and writing data, it is divided into input flow and output flow (from the perspective of the program). The input stream mainly refers to reading data content from the file into the program, that is, reading the file. The output stream mainly refers to the data content in the program output to the file, that is, write file.
- Flows are divided into node flows and processing flows according to their roles. Node flow refers to the flow directly connected with input and output sources. Processing flows mainly refer to flows that need to be built on top of node flows.
architecture
classification | Byte input stream | Byte output stream | Character input stream | Character output stream |
---|---|---|---|---|
Abstract base class | InputStream | OutputStream | Reader | Writer |
Access to the file | FileInputStream | FileOutputStream | FileReader | FileWriter |
Access to an array | ByteArrayInputStream | ByteArrayOutputStream | CharArrayReader | CharArrayWriter |
Access to pipe | PipedInputStream | PipedOutputStream | PipedReader | PipedWriter |
Access string | — | — | StringReader | StringWriter |
Buffer flow | BufferedInputStream | BufferedOutputStream | BufferedReader | BuffereWriter |
Transformation flows | — | — | InputStreamReader | OutputStreamWriter |
Object flow | ObjectInputStream | ObjectOutputStream | — | — |
FilterInputStream | FilterOutputStream | FilterReader | FilterWriter | |
Printing flow | — | PrintStream | — | PrintWriter |
Push back the input stream | PushbackInputStream | — | PushbackReader | — |
Special flow | DataInputStream | DataOutputStream | — | — |
A detailed explanation of the correlation flow
FileWriter class
The basic concept
- The java.io.FileWriter class is mainly used to write text content to a text file.
Common methods
Method statement | Function is introduced |
---|---|
FileWriter(String fileName) | Constructs an object based on the filename specified by the argument |
FileWriter(String fileName, boolean append) | Constructs an object append to the filename specified by the argument |
void write(int c) | Write a single character |
void write(char[] cbuf, int off, int len) | Writes len characters from the specified character array starting at offset OFF to the file output stream |
void write(char[] cbuf) | Writes the cbuf.length character from the specified character array to the file output stream |
void flush() | Refresh the flow |
void close() | Close the flow object and release the related resources |
FileReader class
The basic concept
- The java.io.FileReader class is primarily used to read text data content from text files.
Common methods
Method statement | Function is introduced |
---|---|
FileReader(String fileName) | Constructs an object based on the filename specified by the argument |
int read() | Read the data of a single character and return, -1 means read to the end |
int read(char[] cbuf, int offset, int length) | To read up to len characters from the input stream into an array of characters, returning the number of characters read, -1 to the end |
int read(char[] cbuf) | Read up to cbuf. Length into a character array from this input stream, returning the number of characters read, -1 indicating the end of the read |
void close() | Close the flow object and release the related resources |
FileOutputStream class
The basic concept
- The java.io.FileOutputStream class is primarily used to write raw byte streams such as image data to the output stream.
Common methods
Method statement | Function is introduced |
---|---|
FileOutputStream(String name) | Constructs an object based on the filename specified by the argument |
FileOutputStream(String name, boolean append) | Constructs an object append to the filename specified by the argument |
void write(int b) | Writes the specified byte to this file output stream |
void write(byte[] b, int off, int len) | Writes len bytes from the specified byte array starting at the offset off to the file output stream |
void write(byte[] b) | Writes b.length bytes from the specified byte array to this file output stream |
void flush() | Flushes this output stream and forces out any buffered output bytes |
void close() | Close the flow object and release the related resources |
FileInputStream class
The basic concept
- The java.io.FileInputStream class is mainly used to read image data from the input stream as a byte stream.
Common methods
Method statement | Function is introduced |
---|---|
FileInputStream(String name) | Constructs an object from the file pathname specified by the argument |
int read() | Reads a single byte of data from the input stream and returns it, returning -1 to read to the end |
int read(byte[] b, int off, int len) | Read up to len bytes of data from this input stream into a byte array, returning the number of bytes read, -1 indicating the end of the read |
int read(byte[] b) | Read up to B. length bytes of data from this input stream into a byte array and return the number of bytes read, -1 indicating the end of the read |
void close() | Close the flow object and release the related resources |
int available() | Gets the size of the file associated with the input stream |
BufferedOutputStream class
The basic concept
- Java. IO. BufferedOutputStream class is mainly used to describe the buffer output stream, at this time no need to write each byte of the underlying system.
Common methods
Method statement | Function is introduced |
---|---|
BufferedOutputStream(OutputStream out) | Constructs the object from the reference specified by the argument |
BufferedOutputStream(OutputStream out, int size) | Constructs the object based on the reference and buffer size specified by the argument |
void write(int b) | Write a single byte |
void write(byte[] b, int off, int len) | Writes a portion of the data in a byte array |
void write(byte[] b) | Writes the entire byte array specified by the argument |
void flush() | Refresh the flow |
void close() | Close the flow object and release the related resources |
BufferedInputStream class
The basic concept
- Java. IO. BufferedInputStream class is mainly used to describe the input stream buffer.
Common methods
Method statement | Function is introduced |
---|---|
BufferedInputStream(InputStream in) | Constructs an object from the reference specified by the argument |
BufferedInputStream(InputStream in, int size) | Constructs an object based on the reference and buffer size specified by the argument |
int read() | Read a single byte |
int read(byte[] b, int off, int len) | Read len bytes |
int read(byte[] b) | Read b.length bytes |
void close() | Close the flow object and release the related resources |
BufferedWriter class
The basic concept
- The java.io.BufferedWriter class is mainly used to write single characters, character arrays, and strings to the output stream.
Common methods
Method statement | Function is introduced |
---|---|
BufferedWriter(Writer out) | Constructs the object from the reference specified by the argument |
BufferedWriter(Writer out, int sz) | Constructs the object based on the reference and buffer size specified by the argument |
void write(int c) | Writes a single character to the output stream |
void write(char[] cbuf, int off, int len) | Writes len characters from the character array Cbuf starting with the subscript OFF to the output stream |
void write(char[] cbuf) | Writes the entire contents of the string array Cbuf to the output stream |
void write(String s, int off, int len) | Writes len characters from the parameter s with subscripts starting off to the output stream |
void write(String str) | Writes the contents of the string specified by the argument to the output stream |
void newLine() | Use to write a line separator to the output stream |
void flush() | Refresh the flow |
void close() | Close the flow object and release the related resources |
BufferedReader class
The basic concept
- The java.io.BufferedReader class is used to read individual characters, character arrays, and strings from the input stream.
Common methods
Method statement | Function is introduced |
---|---|
BufferedReader(Reader in) | Constructs the object from the reference specified by the argument |
BufferedReader(Reader in, int sz) | Constructs the object based on the reference and buffer size specified by the argument |
int read() | Reads a single character from the input stream, returning -1 at the end, or the actual character content read otherwise |
int read(char[] cbuf, int off, int len) | Read len characters from the input stream and place them in the cbuf array starting at off, -1 if read to the end, or the actual number of characters read otherwise |
int read(char[] cbuf) | Read the entire array cBUf from the input stream |
String readLine() | Read a line of string and return, returning NULL to read to the end |
void close() | Close the flow object and release the related resources |
PrintStream class
The basic concept
- The java.io.PrintStream class is mainly used to make it easier to print various data contents.
Common methods
Method statement | Function is introduced |
---|---|
PrintStream(OutputStream out) | Constructs the object from the reference specified by the argument |
void print(String s) | Print the contents of the string specified by the argument |
void println(String x) | Used to print a string and terminate the line |
void flush() | Refresh the flow |
void close() | Used to close the output stream and release related resources |
PrintWriter class
The basic concept
- The java.io.PrintWriter class is primarily used to print the formatted form of an object to a text output stream.
Common methods
Method statement | Function is introduced |
---|---|
PrintWriter(Writer out) | Constructs the object from the reference specified by the argument |
void print(String s) | Prints the contents of the string specified by the argument |
void println(String x) | Prints the string and terminates the line |
void flush() | Refresh the flow |
void close() | Close the flow object and release the related resources |
OutputStreamWriter class
The basic concept
- Java. IO. OutputStreamWriter class is mainly used to achieve transform from the characters flow to byte streams.
Common methods
Method statement | Function is introduced |
---|---|
OutputStreamWriter(OutputStream out) | Constructs the object from the reference specified by the argument |
OutputStreamWriter(OutputStream out, String charsetName) | Constructs an object based on the reference and encoding specified by the argument |
void write(String str) | Writes the string specified by the argument |
void flush() | Refresh the flow |
void close() | Close the flow object and release the related resources |
InputStreamReader class
The basic concept
- Java. IO. InputStreamReader class is mainly used to achieve transform from the byte stream to the characters of the flow.
Common methods
Method statement | Function is introduced |
---|---|
InputStreamReader(InputStream in) | Constructs the object from the reference specified by the argument |
InputStreamReader(InputStream in, String charsetName) | Constructs the object based on the reference and encoding specified by the argument |
int read(char[] cbuf) | Reads character data into the array specified by the argument |
void close() | Used to close the output stream and release related resources |
A character encoding
The origin of the code table
- Computers can only read binary data, early on electrical signals. In order to facilitate the computer to identify the characters of each country, it is necessary to describe the characters of each country in the way of number and establish the corresponding relation table, which is called the coding table.
Common coding tables
- ASCII: American standard code for information exchange, represented in the lower 7-bit binary system of one byte.
- Iso8859-1: Latin code table, European code table, using one byte of 8-bit binary representation.
- GB2312: Chinese encoding table, represented by up to two bytes of 16-bit binary.
- GBK: Updated Chinese encoding table to incorporate more Chinese character symbols, using up to two bytes of 16-bit binary representation.
- Unicode: International standard code that combines all characters currently in human use and assigns each character a unique character code. All text is represented by two 16-bit binary bits.
Development of coding
- The UCS Transfer Format (UTF) standard for transmission has emerged. Utf-8 is used to transmit data 8 bits at a time, while UTF-16 is used to transmit data 16 bits at a time. This is code designed for transmission and makes it borderless, so it can display characters from all cultures around the world.
- Unicode simply defines a large, universal character set and assigns a unique number to each character, depending on the character encoding scheme. The recommended Unicode encodings are UTF-8 and UTF-16.
- Utf-8: Variable length encoding that can represent a character in the range of 1 to 4 bytes.
DataOutputStream class
The basic concept
- The java.io.DataOutputStream class is primarily used to write basic data types to an output stream in an appropriate manner.
Common methods
Method statement | Function is introduced |
---|---|
DataOutputStream(OutputStream out) | The OutputStream class is an abstract class whose arguments need to pass subclass objects |
void writeInt(int v) | Used to write the integer specified by the argument to the output stream at once, with high bytes being written first |
void close() | Use to close the file output stream and release related resources |
A DataInputStream class
The basic concept
- The java.io.DataInputStream class is primarily used to read data of primitive data types from an input stream.
Common methods
Method statement | Function is introduced |
---|---|
DataInputStream(InputStream in) | The InputStream class is an abstract class whose arguments need to be passed as subclass objects |
int readInt() | Used to read one integer data at a time from the input stream and return it |
void close() | Use to close the file output stream and release related resources |
The ObjectOutputStream class
The basic concept
- Java. IO. ObjectOutputStream class is mainly used for writing as a whole, all of the content of an object to the output stream.
- Only objects that support the Java.io.Serializable interface can be written to a stream.
- Class implements the java.io.Serializable interface to enable its serialization capabilities.
- Serialization mainly refers to the process of efficiently organizing the relevant information of an object into a sequence of bytes.
Common methods
Method statement | Function is introduced |
---|---|
ObjectOutputStream(OutputStream out) | Constructs the object from the reference specified by the argument |
void writeObject(Object obj) | Use to write the entire object specified by the argument to the output stream |
void close() | Used to close the output stream and release related resources |
ObjectInputStream class
The basic concept
- Java. IO. ObjectInputStream class is mainly used for disposable object from the input stream to read out as a whole.
- Deserialization mainly refers to the process of converting a efficiently organized sequence of bytes back into an object and related information.
Common methods
Method statement | Function is introduced |
---|---|
ObjectInputStream(InputStream in) | Constructs the object from the reference specified by the argument |
Object readObject() | It is used to read an object from the input stream and return the end of the file that cannot be determined by the return value |
void close() | Use to close the input stream and release related resources |
Serialized version number
- The serialization mechanism verifies version consistency by determining the serialVersionUID of a class at run time. During deserialization, the JVM will compare the serialVersionUID in the transmitted byte stream with the serialVersionUID of the corresponding local entity class. If they are the same, the serialVersionUID is considered the same and can be deserialized. Otherwise, an InvalidCastException occurs with an inconsistent serialization version.
Transient keyword
- Transient is a Java language keyword used to indicate that a field is not part of the object’s serialization. When an object is serialized, transient variables are not included in the serialized representation, whereas non-transient variables are included.
Sharing experience
- When hope to write more objects into the file, usually suggested to put more than one object in a collection, and then set the overall written to the output stream as a object, at this time only need to call a readObject method the whole set of data can be read out, thus avoided by judge whether to reach the end of the file for the return value.
RandomAccessFile class
The basic concept
- The java.io.RandomAccessFile class mainly supports read and write operations on randomly accessed files.
Common methods
Method statement | Function is introduced |
---|---|
RandomAccessFile(String name, String mode) | Constructs an object based on the name and schema specified by the argument R: Open it in read-only mode Rw: Open for reading and writing RWD: opens for reading and writing, synchronizing updates of file contents RWS: Opens for reading and writing, synchronizing updates to file content and metadata |
int read() | Read a single byte of data |
void seek(long pos) | Sets the file pointer offset measured from the beginning of this file |
void write(int b) | Writes the single byte specified by the argument |
void close() | Used to close the stream and release related resources |