@[toc]

preface

Java I/O streams have many classes. The most common ones are InputStream and OutputStream, which are the two top-level parent classes of the byte stream (and of course interfaces), FileReader and FileWriter. These are the two top-level parent classes of the character stream

Input and output can be understood as relative to memory or screen. Read from disk to memory is input, and write to disk is output. InputStream and FileReader are used to read data, while OutputStream and FileWriter are used to write data to disk

Here is a picture from the rookie tutorial that clearly shows the parent-child relationship between the classes

Byte stream

The usual OutputStream and InputStream are output streams and input streams, both of which are abstract classes, and then we use FileOutStream and FileInputStream, There are also BufferedOutputStream and BufferedInputStream, which are buffers. The constructor takes the parameter types of OutputStream and InputStream, respectively

To append the byte stream, pass true to the second argument at constructor time, and default false to complete overwrite

OutputStream byte OutputStream

The java.io package has classes for byte output streams, of which OutputStream is an abstract class

Most programmers use FileOutputStream and BufferedOutputStream the most

Int is the number of the asCall code. Byte is a single byte. Note that char is not the type

If we do not use buffer, it is ok, if we use buffer.close() must be added, otherwise the bytes in buffer will not be saved to disk!!

public class IOOperation {
    @Test
    public void fun(a) throws IOException {
        FileOutputStream stream = new FileOutputStream("src\\test\\java\\temp.txt");
        BufferedOutputStream buffer = new BufferedOutputStream(stream);
        buffer.write(new byte[] {'a'.'b'.'c'}); buffer.close(); stream.close(); }}Copy the code

Pay attention to the point

  • If a buffer is used, add ‘buffer.close()’ or the contents of the buffer will not be written to disk!

InputStream indicates a byte InputStream

The java.io package has classes for these byte output streams. InputStream is an abstract class

The ones most programmers use most often are FileInputStream and BufferedInputStream

public class IOOperation {
    @Test
    public void fun(a) throws IOException {
        FileInputStream stream = new FileInputStream("src\\test\\java\\temp.txt");
        BufferedInputStream buffer = newBufferedInputStream(stream); System.out.println(buffer.read()); System.out.println(buffer.read()); System.out.println(buffer.read()); System.out.println(buffer.read()); buffer.close(); stream.close(); }}Copy the code

Pay attention to the point

  • If buffer is used and buffer.close() is added or not, you can see that the console has normal output, which is different from the output stream
  • The read method returns -1 if no bytes are read. The read method returns an int
  • After each use of the read method, byte by byte is read backwards

Characters of the flow

Writer is a character output stream, and Reader is a character input stream. Both are abstract classes. The subclasses OutputStreamWriter and InputStreamReader are entity classes

To append a character stream, pass true to the second argument at constructor time, and default false to complete overwriting

Writer character output stream

Character output stream

In general, we use FileWriter and BufferedWriter the most

Here is a small example to show the append file. Go to the next line and say aaabbbCCC

public class IOOperation {
    @Test
    public void fun(a) throws IOException {
        FileWriter writer = new FileWriter("src\\test\\java\\temp.txt".true);
        BufferedWriter buffer = new BufferedWriter(writer);
        buffer.newLine();
        buffer.write("aaa");
        buffer.append("bbb").append("ccc"); buffer.close(); writer.close(); }}Copy the code

Pay attention to the point

  • Write does not return a value and cannot accept NULL. Append accepts null, writes to disk as a null string, returns a value, and returns the stream itself. So you can keep going.append("").append("")In the form of
  • Buffer must be added close; otherwise, the disk will have no content

Reader character input stream

Character input stream

In fact, the most common thing we use is FIleReader and BufferedReader

Let’s just look at the example below

public class IOOperation {
    @Test
    public void fun(a) throws IOException {
        FileReader reader = new FileReader("src\\test\\java\\temp.txt");
        BufferedReader buffer = newBufferedReader(reader); System.out.println(buffer.read()); System.out.println(buffer.readLine()); buffer.close(); reader.close(); }}Copy the code

Pay attention to the point

  • Similar to the byte stream, the input stream is not closed when using buffer, and the contents of the file can be read to the console when turning off buffer, whereas the output stream must be closed with buffer
  • The read method returns an int, which is a one-character AScall code, and the readLine method returns a string of the entire line following the current position
  • The read method returns -1 if it can’t read a character, and the readLine method returns null if it can’t read a line

The difference between

A byte stream reads a byte, and a character stream reads a character

Character streams can be encoded differently

Character streams are more flexible, and text files are recommended

Usage scenarios

For text files, I recommend using character streams, which are easy to use. For images and other files (files on computers are binary files, which can be byte streams), I recommend using byte streams

Other Points for attention

  • The flush() operation of the Write output stream means that the buffer is printed to disk only when the IO operation has a buffer. If you flush directly, you can manually flush the buffer to disk once the buffer is not full

  • The byte stream setting encoding method can be done through the constructor of InputStreamReader or OutputStreamWriter. The second argument passes the encoding name. The two parent classes are Reader and Writer. The first argument to the InputStreamReader constructor is of type InputStream. The first argument to the InputStreamReader constructor is of type InputStream. InputStream is part of the byte stream family, and I looked at the source code and found that if it didn’t pass the second encoding, it would execute this code charset.defaultCharset ().name(), which reads the configured encoding, or the encoding of its environment

  • If it’s Chinese, you can read the Java byte stream but you might not know what it’s reading, like an “ah” that reads 229 149 138 on the screen