This is the 9th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021.

flow

Why do you always talk about IO stream? What does this flow mean?

The word flow is also often used in the field of esports. Everybody said, oh, the way the team used to go up and sell their heads to gain an advantage was called the sacrificial flow.

In a Java environment, a stream is the process by which data is read from an input source to a program and written from a program to an output source.

Data, like water, comes and goes, but leaves something behind. What is left depends on what the program does with the data.

In Java, there are different classification standards.

According to the flow of data, it can be divided into:

  • The input stream:

    Data flow from data source to application;

  • The output stream:

    Data flows from the program to the target output.

According to the type of data obtained by unit, it can be divided into:

  • Byte stream

    The value is obtained in bytes

  • Characters of the flow

    It is named XXXStream in units of characters.

According to different processing objects, it can be divided into:

  • The node flow:

    Directly from the source or destination, speaking, reading and writing data, such as a FileInputStream/FileOutputStream;

  • Processing flow (packaging flow) :

    Other streams are processed to make node streams more usable, such as many buffer-beginning streams.

IO abstract class

The java.io package contains almost all the classes needed for input and output of operations. The abstract parent of all IO stream classes is:

  • InputStream (byte InputStream)
  • OutputStream (byte OutputStream)
  • Reader (character input stream)
  • Writer (character output stream)

It’s four dads, and every time you use it, make it as specific as possible.

The figure below is from cainiao website (Java)The branch following each parent class is the class we call when we use it.

You can see that many classes have good symmetry:

  • Below the character stream

A. Writer B. Reader C. Writer D. Writer

  • Below the byte stream

Write: OutputStream, read: InputStream.

Operations including each of the following also come in pairs:

BufferedReader — BufferedWriter, InputStreamReader — OutputStreamWriter.

Commonly used method

  1. .close()

It’s very important, otherwise when the resource is opened, it takes up memory. Especially open large files, or open multiple files. We should have the consciousness of memory control from small white beginning.

  1. .read()

The convention of the InputStream class is that read returns values ranging from -1 to 255int.

  1. .write()

Other common operations on File: check the path (current, absolute), whether the File exists, whether the File isHidden (isHidden), whether it is empty, and so on.

In the code

public class FirstDemo {
    public static void main(String[] args) {
        // Create a byte input stream
        FileInputStream file = null;
        try {
            file = new FileInputStream("/Users/fang/CODE/F/src/com/ftech/TestFileIO/a.rtf");
            int temp = 0;
            StringBuilder sb = new StringBuilder();
            while((temp = file.read()) ! = -1) {
                System.out.println(temp);
                sb.append((char)temp);
            }
            System.out.println(sb.toString());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(file! =null) {
                try {
                    file.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
Copy the code

Don’t know how to create with apple’s own softwaretxt, so I randomly generated a text file and saved it in the format ofrtf. IDEA is very powerful in showing me where to use exception handling. Here are twocatchThe exact location to be reminded at compile time.

There is ABC in the file, so it is easy to finish reading, and the numbers read out are: ABC corresponds to the ASCII values 97, 98, 99, and then the last line is -1.

But when I read it, INSTEAD of writing the loop at first, I changed the contents of the file and it read the same:

I never read -1, but I’m using itwhile + sbLater, only to find the original file in front of the inexplicable so many characters:There’s only one “A” in my file. There are more than 300 lines left uncut. Looking forward to the big guy solving the puzzle. May bertfThe particularity of the format ~