Java I/O flow
1, Classification of flow
- According to different operation data units, it is divided into byte stream (8 bit) and character stream (16 bit)
- According to the flow direction of data, it can be divided into input flow and output flow
Abstract base class | Byte stream | Character stream |
---|---|---|
Input stream | InputStream | Reader |
Output stream | OutputStream | Writer |
The names of subclasses derived from these four classes (abstract classes) are suffixed by their parent class names
IO stream system
classification | Byte input stream | Byte output stream | Character input stream | Character output stream |
---|---|---|---|---|
Abstract base class | InputStream | OutputStream | Reader | Writer |
access files | FileInputStream | FileOutputStream | FileReader | FileWriter |
Access array | ByteArrayInputStream | ByteArrayOutputStream | CharArrayReader | CharArrayWriter |
Access pipeline | PipedInputStream | PipedOutStream | PipedReader | PipedWriter |
Access string | StringReader | StringWriter | ||
Buffer stream | BufferedInputStream | BufferedOutputStream | BufferdReadier | BufferWriter |
Conversion flow | InputStreamReader | OutputStreamWriter | ||
Object flow | ObjectInputStream | ObjectOutput Stream | ||
FilterInputStream | FilterOutputStream | FilterReader | FilterWriter | |
Print stream | PrintStream | PrintWriter | ||
Push back input stream | PushbackInputStream | PushbackReader | ||
Special flow | DataInputStream | DataOutputStream |
The difference between character stream and byte stream
- The basic unit of byte stream operation is byte; The basic unit of character stream operation is Unicode symbol.
- The byte stream will not use the buffer during operation, while the character stream will use the buffer during operation
- Byte stream can be used for any type of object, including binary objects, while character stream can only process characters or strings
2, File operation
2.1 FileReader
Construction method
FileReader(File file) //Construct a FileReader object according to the given File object. FileReader(FileDescriptor fd) //Construct a FileReader object according to the given file name. FileReader(String fileName) //Construct a FileReader object according to the given FileDescriptor
use
int read() throws IOException //Read a single character and return an int variable to represent the read character. The return value of - 1 is equivalent to reading it int read(char [] c, int offset, int len)//Read characters to c array and return the number of characters read void close() throw IOException //Close flow
2.2 FileWriter
Construction method
FileWriter(String fileName) throws IOException //Construct a FileWriter object according to the given file name. FileWriter(String fileName,boolean append) throws IOException//The FileWriter object is constructed based on the given file name and a boolean value indicating whether to attach write data. append: a boolean value. If true, the data will be written to the end of the file instead of the beginning of the file. FileWriter(File file) throws IOException //Construct a FileWriter object according to the given File object.
common method
void write(int c) //Write a character void write(String str)//Write string void write(String str, int off, int len) //Write part of a string. void write(char[] cbuf, int off, int len) //Writes part of a character array. void flush() throws IOException //Refresh the stream. void close() throws IOException //Refresh before closing the stream
2.3 FileInputStream
Construction method
FileInputStream(File file) throws FileNotFoundException //Construct a FileInputStream object according to the given File object. FileInputStream(String name) throws FileNotFoundException //Construct a FileInputStream object according to the given file name. FileInputStream(FileDescriptor fd) //Construct a FileInputStream object according to the given FileDescriptor
common method
int read() throws IOException //Convert the read byte type 8-bit binary into hexadecimal int type and return, - 1 indicates that the reading is completed int read(byte[] b) throws IOException//Read b.length bytes from the input stream into the byte array, and return the total number of bytes read into the buffer - 1 indicates that the reading is complete int read(byte[] b,int off,int len) throws IOException //Read up to len bytes from the input stream into the byte array, and store bytes from the off position of the array void close() throws IOException //Close flow
2.4 FileOutputStream
Construction method
FileOutputStream(File file) throws FileNotFoundException //Construct a FileOutputStream object according to the given File object. FileOutputStream(File file,boolean append) throws FileNotFoundException{} //If the second parameter is true, the byte will be written to the end of the file instead of the beginning. When the second parameter is false, the original content will be overwritten FileOutputStream(String name) throws FileNotFoundException{}//Construct a FileOutputStream object according to the given file name object. FileOutputStream(String name,boolean append) throws FileNotFoundException
common method
void write(int b) throws IOException //Writes a specified byte to the output stream of the file void write(byte[] b) throws IOException //Writes the b.length bytes in the specified byte array to the output stream void write(byte[] b,int off,int len) throws IOException //Writes len bytes in the specified byte array starting from offset off to the output stream void close() throws IOException //Close flow
The getBytes method under the common String class converts the String into a byte array
3, Buffer stream
The difference between Buffered and FIle in FIle operation
- BufferedReader and BufferedWriter will carry an 8kb byte buffer in memory
- BufferedReader provides the readLine() method
- The BufferedReader class will try to extract more bytes than the current operation needs, so as to improve the efficiency in more cases
3.1 BufferedReader
Construction method
BufferedReader br = new BufferedReader(new FileReader("FileName"));//First, create a FileReader object and pass the FileReader to BufferedReader BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("FileName"), "utf-8"));//Create a BufferedReader object using the wrapped InputStream class
common method
int read() throws IOException; //Read one or more bytes and return a character. When reading to the end of the file, return - 1 int read(byte[] b,int off,int len) throws IOException //Read up to len bytes from the input stream into the byte array, and store bytes from the off position of the array String readline() //Read a line of data, store it in the string and return it, excluding line breaks void close() //Close flow
Garbled code problem
When the BufferedReader packaging FileReader reads a file, if the file code is different from the project code, there will be garbled code
resolvent:
Use the BufferedReader that wraps the InputStreamReader to read the file
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("FileName"), "utf-8")); //BufferedReader is only responsible for reading into its internal buffer, and the decoding is completed by InputStreamReader.
3.2 BufferedWriter
Construction method
BufferedWriter bw = new BufferedWriter(new FileWriter("FileName"));//First, create a FileWriter object and pass the FileWriter to BufferedWriter
common method
void write(int c) //Write a single character. void write(char[] c, int off, int len) //Write a part of the character array with the length of len from off void write(String s, int off, int len) //Write a part of the string with length len from off. void newLine() //Write a line separator. void flush() throws IOException //Flush the buffer of the stream. void close() throws IOException //Refresh before closing the stream
3.3 BufferedInputStream
Construction method
BufferedInputStream input = new BufferedInputStream(new FileInputStream("FileName"));//Create a bufferedinputstream object using the wrapped InputStream class
common method
int read() throws IOException // Read next byte int read( byte [] b, int off, int len) throws IOException // Writes the data in the buffer to the byte array b. off is the starting position of byte array b, and len is the write length void mark(int readlimit) //Mark the current position in the buffer. readlimit is the maximum number of bytes that can be read after mark void reset() throws IOException // Resets the current position in the buffer to the position marked by mark() void close() throws IOException //Close flow
3.4 BufferedOutputStream
Construction method
BufferedOutputStream output = new BufferedOutputStream(new FileOutputStream("FileName"));//Create a bufferedoutputstream object using the wrapped OutputStream class
common method
void write(int b)throws IOException //Write one byte at a time void write(byte[] b,int off,int len) throws IOException //Write a byte string with length len from byte array off void flush() throws IOException //Refresh buffer void close() throws IOException //Refresh before closing the stream