Can't use IO stream yet? Then you're out

Posted by Im Jake on Sat, 13 Nov 2021 02:24:16 +0100

catalogue

Byte stream

Byte buffer stream

Character stream

Character buffer stream

Usage scenario

IO stream:

        1) IO: input / output

        2) Stream: it is an abstract concept, which is the general name of data transmission. In other words, the transmission of data between devices is called stream, and the essence of stream is data transmission

        3) IO stream is used to deal with data transmission between devices. Common applications: file replication; File upload; File download

See the IO flow framework diagram below:

This article mainly introduces how to use byte stream and character stream to read and write data, without too much theory

Byte stream

Next, let's understand the byte stream write data method and read data method through the code:

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

        /**Create byte output stream object from data source*/
        FileOutputStream fos = new FileOutputStream("D:\\IO\\1.txt");

        /**void write(int b): Writes the specified bytes to this file output stream*/
//        fos.write(97);
//        fos.write(98);
//        fos.write(99);
//        fos.write(100);
//        fos.write(101);

        /**void write(byte[] b): Writes b.length bytes from the specified byte array to this file output stream*/
//        byte[] bys = {97, 98, 99, 100, 101};
//        fos.write(bys);

        /**void write(byte[] b, int off, int len): Write len bytes to the file output stream starting from the specified byte array and starting from offset off*/
        //byte[] getBytes(): returns the byte array corresponding to the string
        byte[] bys = "abcde".getBytes();
        fos.write(bys,1,3);

        /**Release resources*/
        fos.close();

        /**Create byte input stream object from data source*/
        FileInputStream fis = new FileInputStream("D:\\IO\\1.txt");
        int by;
        /**
            fis.read(): Read data
            by=fis.read(): Assign the read data to by
            by != -1: Judge whether the read data is - 1
         */
        while ((by=fis.read())!=-1) {
            System.out.print((char)by);
        }

        /** public int read(byte[] b)Read one byte array at a time*/
//        byte[] bys = new byte[1024]; //1024 and its integer multiples
//        int len;
//        while ((len=fis.read(bys))!=-1) {
//            System.out.print(new String(bys,0,len));
//        }
        /**Release resources*/
        fis.close();
    }
}

Byte buffer stream

Buffered streams are designed to speed up reading and writing

Let's learn about byte buffered output stream and byte buffered input stream through code:

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

        //Byte buffered output stream: BufferedOutputStream(OutputStream out)
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("D:\\IO\\1.txt"));
        //Write data
        bos.write("hello\r\n".getBytes());
        bos.write("world\r\n".getBytes());
        //Release resources
        bos.close();

        //Byte buffered input stream: BufferedInputStream(InputStream in)
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:\\IO\\1.txt"));

        //Read one byte of data at a time
//        int by;
//        while ((by=bis.read())!=-1) {
//            System.out.print((char)by);
//        }

        //Read one byte array data at a time
        byte[] bys = new byte[1024];
        int len;
        while ((len=bis.read(bys))!=-1) {
            System.out.print(new String(bys,0,len));
        }

        //Release resources
        bis.close();
    }
}

Character stream

Because byte stream is not particularly convenient to operate Chinese, Java provides character stream

Character stream = byte stream + encoding table

  Let's understand the methods of writing data and reading data of character stream through code:

public class Demo2 {
    public static void main(String[] args) throws IOException {
        OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("D:\\IO\\1.txt"));
        //void write(int c): write a character
//        osw.write(97);
//        osw.write(98);
//        osw.write(99);

        //Void write (char [] cbuf): write a character array
        char[] chs = {'a', 'b', 'c', 'd', 'e'};
//        osw.write(chs);

        //void write(char[] cbuf, int off, int len): writes a part of the character array
//        osw.write(chs, 0, chs.length);
//        osw.write(chs, 1, 3);

        //void write(String str): write a string
//        osw.write("abcde");

        //void write(String str, int off, int len): write a part of a string
//        osw.write("abcde", 0, "abcde".length());
        osw.write("abcde", 1, 3);

        //Release resources
        osw.close();

        InputStreamReader isr = new InputStreamReader(new FileInputStream("D:\\IO\\1.txt"));

        //int read(): read data one character at a time
//        int ch;
//        while ((ch=isr.read())!=-1) {
//            System.out.print((char)ch);
//        }

        //int read(char[] cbuf): read one character array data at a time
        char[] chs1 = new char[1024];
        int len;
        while ((len = isr.read(chs1)) != -1) {
            System.out.print(new String(chs1, 0, len));
        }

        //Release resources
        isr.close();
    }
}

Character buffer stream

Unique features:

BufferedWriter: void newLine() writes a line separator. The line separator string is defined by the system property

BufferedReader: String readLine() reads a line of text. The result is a string containing the contents of the line, excluding any line termination characters. It is null if the end of the stream has been reached

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

        //Create character buffered output stream
        BufferedWriter bw = new BufferedWriter(new FileWriter("D:\\IO\\1.txt"));

        //Write data
        for (int i = 0; i < 10; i++) {
            bw.write("hello" + i);
            //bw.write("\r\n");
            bw.newLine();
            bw.flush();
       }

        //Release resources
        bw.close();

        //Create character buffered input stream
        BufferedReader br = new BufferedReader(new FileReader("D:\\IO\\1.txt"));

        String line;
        while ((line=br.readLine())!=null) {
            System.out.println(line);
        }

        br.close();
    }
}

Usage scenario

Example: use byte stream to copy pictures, video, audio, etc

Generally, when copying binary files such as picture, video and audio, byte buffer stream will be adopted, and one byte array will be read and written at a time

public class Demo {
    public static void main(String[] args) throws IOException {
         //The byte buffer stream reads and writes one byte array at a time
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:\\IO\\Byte stream copy picture.jpg"));
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("D:IO\\Byte stream copy picture.jpg"));

        byte[] bys = new byte[1024];
        int len;
        while ((len=bis.read(bys))!=-1) {
            bos.write(bys,0,len);
        }

        bos.close();
        bis.close();
    }
}

Copying audio is the same as video

Example: copy a plain text file using a character stream

public class Demo {
    public static void main(String[] args) throws IOException {
        //Create a character buffered input stream object from the data source
        BufferedReader br = new BufferedReader(new FileReader("D:\\IO\\test.txt"));
        //Creates a character buffered output stream object based on the destination
        BufferedWriter bw = new BufferedWriter(new FileWriter("D:\\IO\\Copy.txt"));
        //Read and write data, copy files
        //Using the special function of character buffer stream
        String line;
        while ((line=br.readLine())!=null) {
            bw.write(line);
            bw.newLine();
            bw.flush();
        }

        //Release resources
        bw.close();
        br.close();
    }
}

Special stream please see the next blog post!!!

Topics: Java Back-end