Java IO stream byte stream overview-1

Insert picture description here

I / O

What is I / O

  • In life, when you plug a U disk into your computer and can copy a video to your computer, this data transmission process
  • We can think of this kind of data transmission as a kind of data flow. According to the flow direction, taking memory as the benchmark, it is divided into input (input) and output (output), that is, the input stream when it flows to memory, and the output stream that flows out of memory
  • The IO operation in Java mainly refers to the content under the java.io package. Input and output operations are performed.ReadData, the output is also calledWrite outdata

The following figure shows the operation relationship between hard disk and memory! ! !
Insert picture description here

I / O classification

According to the data flow direction: input flow and output flow

  • Input stream: a stream that reads data from other devices into memory
  • Output stream: a stream that writes data from memory to other devices

According to the type of data is divided into: byte stream and character stream The
following figure is their top parent class
Insert picture description here

Byte output stream [OutputStream]

When all file data (text, pictures, videos) are stored again, they are stored in the form of binary digits, all one by one byte, the transmission time is the same, so the byte stream can transmit any file data
and then operate the stream is even, we need time It is clear that no matter what Yang's stream object is used, the underlying transmission is always binary data. The
java.io.OutputStream abstract class represents the parent class of all classes that represent byte output streams, writes the specified byte information to the destination, and defines The basic common function method of byte stream

Insert picture description here

Note: The close method, when the operation of the stream is completed, this method must be called to release the system resources!

public class Demo01OutputStream {

    public static void main(String[] args) throws IOException {

        FileOutputStream out = new FileOutputStream("F:\\ziwozongjie.txt");
        String content = "囧囧,囧囧是世界上好看的女人!!!";
        out.write(content.getBytes());
        out.close();
    }
}

Looking at the F drive will create a ziwozongjie.txt file for you, and write the following sentence to the file

The principle of writing data (memory-> hard disk)

  • java program-> JVM (java virtual machine-> OS (operating system)-> OS call method to write data-> write data to file

Principle of depositing data

public class Demo01OutputStream {

    public static void main(String[] args) throws IOException {
        FileOutputStream out = new FileOutputStream("F:\\jiongjiong.txt");
        out.write(97);
        out.close();
    }
}

When writing data, the decimal system will be converted into binary system, the corresponding stored is the binary system 97 is 1100001
when we open the text editor (any text editor, notepad or nodepad ++) will query the encoding Table, convert bytes to characters.
If it is stored in 0-127, it will query the ASCII code table and our 97 corresponds to the English letter a. If it is other values, the system default code table (Chinese system GBK or UTF-8) is queried. )

The result is lowercase a
Insert picture description here

! ! ! note! ! !

  • When we call the method of writing multiple bytes at once void write (byte [] b)
  • If the first byte is a positive number (0-127), the ASCII code table will be queried when displayed
  • If the first byte is negative, then the first byte and the second byte, two bytes form a Chinese display, query the system default code table (GBK)
public class Demo01OutputStream {

    public static void main(String[] args) throws IOException {

        FileOutputStream out = new FileOutputStream("F:\\jiongjiong.txt");
        byte[] b = {-65, -66, -67, 68, 79};
        out.write(b);
        out.close();
    }
}

Insert picture description here

The result is not garbled, but corresponds to the Chinese characters in the GBK table

Append write and resume

Insert picture description here

  • String name, File file Write data destination
  • boolean append true: will not overwrite the original file, continue to append to the end of the file false: create a new file, overwrite the original file

! ! ! Code on! ! !

public class Demo02OutputStream {

    public static void main(String[] args) throws IOException {

        FileOutputStream fot = new FileOutputStream("F:\\cccc.txt", true);
        String content = "您好!豆豆";
        for (int i = 0; i < 10; i++) {
            fot.write(content.getBytes("utf-8"));
            fot.write("\r\n".getBytes());
        }
        fot.close();

    }
}

Insert picture description here

Byte input stream [InputStream]

The java.io.InputStream abstract class is a superclass representing all classes of byte input streams. It can read byte information into memory and defines basic shared function methods for byte input streams.

Insert picture description here

Note: The close method, when the operation of the stream is completed, this method must be called to release the system resources!

! ! ! Code on! ! !

public class Demo03FileInputStream {

    public static void main(String[] args) throws IOException {

        FileInputStream fis = new FileInputStream("F:\\cccc.txt");
        byte[] bytes = new byte[1024];
        int len = 0;
        while ((len = fis.read(bytes)) != -1) {
            System.out.println(new String(bytes, 0, len));
        }

    }
}

! ! ! result! ! !
Insert picture description here

Demo file copy

public class Demo04FileInputStream {

    public static void main(String[] args) throws IOException {

        try (
                FileInputStream fis = new FileInputStream("F:\\cccc.txt");
                FileOutputStream fos = new FileOutputStream("F:\\dddd.txt");
        ) {
            int len = 0;
            byte[] bytes = new byte[1024];
            while ((len = fis.read(bytes)) != -1) {
                fos.write(bytes, 0, len);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("复制成功");


    }
}

File copied successfully
Insert picture description here

Note: Here I used jdk8's feature Lambda expression try ... with ... resource so there is no need to close the stream in the finally block, jdk will help us optimize this code to try ... catch ... finally

Published 24 original articles · praised 33 · visits 2391

Guess you like

Origin blog.csdn.net/weixin_41241629/article/details/104323586