Java IO content
The classification structure according to the operation mode is as follows:
The operation object classification structure is as follows:
The difference between byte stream and character stream:
The byte stream reads a single byte, and the character stream reads a single character (a character has a different byte depending on the encoding, for example, utF-8 is 3 bytes, Chinese is 2 bytes). Byte streams are used to process binary files (pictures, MP3s, video files), and character streams are used to process text files (think of as special binary files, using some kind of encoding, that can be read by humans).
IO classes and related methods
The four basic abstract classes in IO are Reader, Writer, InputStream, and OutputStream. The most basic method is a read() method, a writer() method.
InputStream class
methods | Methods to introduce |
---|---|
public abstract int read() | readNext byteThe current stream returns -1 after reading |
public int read(byte b[]) | Put the read data into a byte array, which is actually implemented using the following method, where off is 0 and Len is the length of the array |
public int read(byte b[], int off, int len) | Read len bytes of data from the off position and place it in the byte array. The stream ends with a value of -1.link ) |
public long skip(long n) | Skipping a specified number of bytes without reading is like skipping the beginning and end of a movie |
public int available() | Returns the number of bytes readable |
public void close() | After reading, close the stream and release the resource |
public synchronized void mark(int readlimit) | Mark the currentThe input stream You can also start reading from this position next time, depending on whether the current stream supports it, you can use the markSupport() method to determine |
public synchronized void reset() | Reset the read position to the last mark position |
public boolean markSupported() | Determine whether the current stream supports the tag stream and use it in conjunction with the above two methods |
OutputStream class
methods | Methods to introduce |
---|---|
public abstract void write(int b) | Write a byte, and the argument here is an int, corresponding to the above method. The 32 bits of int are written only to the lower 8 bits, and the higher 24 bits are discarded. |
public void write(byte b[]) | Writes all the bytes in the array, similar to the corresponding read() method above, which actually calls the following method. |
public void write(byte b[], int off, int len) | Writes len length bytes from the byte array starting at the off position |
public void flush() | Force a flush to write data from the buffer |
public void close() | Close the output stream. The stream can no longer output data after it is closed |
Reader class
methods | Methods to introduce |
---|---|
public int read(java.nio.CharBuffer target) | Read bytes into the character cache |
public int read() | Reading a single character |
public int read(char cbuf[]) | Reads characters into the specified char array |
abstract public int read(char cbuf[], int off, int len) | Reads len characters from the off position into the char array |
public long skip(long n) | Skips the number of characters of specified length |
public boolean ready() | Similar to the available() method above |
public boolean markSupported() | Determines whether the current stream supports tag streams |
public void mark(int readAheadLimit) | Mark the read position, and you can start reading from it next time. Before using it, check whether the current stream supports it, using the markSupport() method |
public void reset() | Reset the read position to the last mark position |
abstract public void close() | Close the stream to release the associated resources |
Writer class
methods | Methods to introduce |
---|---|
public void write(int c) | Write a character |
public void write(char cbuf[]) | Writes to a character array |
abstract public void write(char cbuf[], int off, int len) | Writes len of characters from the off position of the character array |
public void write(String str) | Write a string |
public void write(String str, int off, int len) | Writes len number of characters from the off position of the string |
public Writer append(CharSequence csq) | Appends to inhale a character sequence |
public Writer append(CharSequence csq, int start, int end) | Appending part of a character sequence, starting at start and ending at end |
public Writer append(char c) | Appends to a 16 – bit character |
abstract public void flush() | Force a flush to write data from the buffer |
abstract public void close() | Close the output stream. The stream can no longer output data after it is closed |
Precautions for the close method
Some java.io output classes contain a flush method. When close is called, the class automatically executes the flush method. There is no need to display the flush method before calling close.
When a stream1 is linked to stream2 through the constructor of another stream2, closing stream2 automatically closes the underlying Stream1 resource. If multiple streams are linked together, closing the last created stream will automatically close all the underlying stream resources. If the output cache stream and input cache stream exist at the same time, the output cache stream is closed first, and the input cache stream is closed later.