Actual analysis of Java file input stream and output stream (suitable for Xiaobai)

        In the Java API, an object that can read a byte sequence from it is called an input stream, and an object that can write a byte sequence from it is called an output stream. The two streams we involve most are InputStream and OutputStream. An abstract class. This article is my experience of reading "Java Core Technology Volume 2". The code is written by me in combination with specific scenarios. I believe that readers who are beginners of input stream and output stream will be able to accurately distinguish the two streams after reading it. Finally, it is not easy to create, like, collect, and follow three links, crab and crab~

One, input stream and file input stream

1. Explore the read method

       First, the InputStream class has an abstract method: abstract int read()

       This method will read a byte and return the byte that was read, or return -1 when the end of the input is reached. Note: Since InputStream is rarely used directly in practice, we mostly use subclasses of InputStream, among which the most commonly used is FileInputStream, in which the FileInputStream class overrides the read method of the InputStream class.

Here is the JDK source code as a reference:

       InputStream's read() source code:

       @Override
            public int read() throws IOException {
                ensureOpen();
                return -1;
            }

        The comment part in the figure below says: Read one byte of data from this input stream. If there is no input available for reading, this method will block. Return the next byte. If the end of the file is reached, -1 is returned.

    /**
     * Reads a byte of data from this input stream. This method blocks
     * if no input is yet available.
     *
     * @return     the next byte of data, or {@code -1} if the end of the
     *             file is reached.
     * @throws     IOException  if an I/O error occurs.
     */
    public int read() throws IOException {
        return read0();
    }

Specific examples are given below.

Open IDEA (don't use the evil Eclipse, this thing is not good at all), create a text file Test.txt, the content of the file is: Hello!

To create the Test.java file, you must first create a file object: File file = new File("E:\\spraying problem\\Test.txt");

public class Test {
    public static void main(String[] args) throws IOException {
        File file = new File("E:\\喷涂问题\\Test.txt");
        FileInputStream in=new FileInputStream(file);
        int read = in.read();
        System.out.println(read);
    }
}

Output: 72.

         Maybe someone is curious why a number is output? In fact, this is the ASCII code corresponding to the H letter!

After looking up the table, the corresponding ASCII codes of H,e,l,l,o,! are 72 101 108 108 111 33, so we only need to convert the integer type to the character type.

public class Test {
    public static void main(String[] args) throws IOException {
        File file = new File("E:\\喷涂问题\\Test.txt");
        FileInputStream in=new FileInputStream(file);
        int read = in.read();
        char text=(char)read;
        System.out.println(text);
    }
}

         So you can output: H

 

2, readAllBytes method

            If you only use the read method, such as in the above example, you have to read'H','E','L'..., obviously, it is too slow and inconvenient. So starting from Java9, a readAllBytes() method is provided, which directly reads all characters in the input stream into a byte array . Then you can output text information at once through conversion!

import java.io.*;
public class Test {
    public static void main(String[] args) throws IOException {
        File file = new File("E:\\喷涂问题\\Test.txt");
        FileInputStream in=new FileInputStream(file);
        byte[] bytes = in.readAllBytes();
        for (byte Byte : bytes) {
            char c=(char)Byte;
            System.out.print(c);
        }
    }
}

Console output:

 

 

Second, the file output stream

        Now let's explain the overall steps of the previous input stream in a continuous way:

1. Create a text file and enter a paragraph of text Hello!

2. Read the text information into the byte array byte[] through the input stream.

---------------------------------------------Dividing line--- ---------------------------------------------

        What about the output stream?

        Of course it's the other way around! ! That is , output the data already in the byte array to an empty text file .

Give an example on the basis of the previous one:

1. First create an empty text file ReadFromTest.txt.

2. Create an object file2 representing an empty file and call the out.write() method. It can be understood that the output stream has been bound to the new file, and writing the byte array to the output stream is equivalent to writing to the new file. .

import java.io.*;

public class Test {
    public static void main(String[] args) throws IOException {
        File file1 = new File("E:\\喷涂问题\\Test.txt");
        FileInputStream in=new FileInputStream(file1);
        byte[] bytes = in.readAllBytes();
//以上部分的代码将原来Test.txt中的hello!转移到字符数组bytes中了

        File file2 = new File("E:\\喷涂问题\\ReadFromTest.txt");
        FileOutputStream out = new FileOutputStream(file2);
        out.write(bytes);
    }
}

3. Run the above code and you will find that the text message Hello! appears in the new file ReadFromTest.txt.

If you don’t know why there is a garbled code behind, friends who know how to eliminate it can comment in the message area!

Guess you like

Origin blog.csdn.net/Zhongtongyi/article/details/108022372