IO stream (byte stream & character stream)

Posted by wazo00 on Sat, 19 Feb 2022 21:26:18 +0100

IO: refers to the flow of data transfer between devices
Classification by flow direction:
Input stream
Output stream
Classification by data type:
Byte stream
A: byte input stream InputStream
            FileInputStream:
FileInputStream fis = new FileInputStream("file name of read data");
Byte buffered input stream: BufferedInputStream
Bufferedinputstream bis = new bufferedinputstream (New FileInputStream);
/ / Note: the file here must exist in advance
There are two ways to read data from byte input stream:
1. Read one byte at a time
            int b = 0;
            while((b=bis.read())!=-1){
                System.out.print((char)b);
            }
            bis.close();

2. Read one byte array at a time
            byte[] bytes = new byte[1024];
            int length = 0;
            while((length = bis.read(bytes))!=-1){
                System.out.print(new String(bytes,0,length));
            }
            bis.close();

package com.shujia.wyh.day23;


import java.io.BufferedInputStream;
import java.io.FileInputStream;

public class BufferedInputStreamDemo1 {
    public static void main(String[] args) throws Exception {
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("g.txt"));

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

        System.out.println("======================");

        byte[] bytes = new byte[1024];
        int length = 0;
        while ((length = bis.read(bytes)) != -1) {
            System.out.println(new String(bytes, 0, length));
        }

        //Release resources
        bis.close();


    }

}

B: byte output stream OutputStream
            FileOutputStream:
FileOutputStream fos = new FileOutputStream("write file name");
Byte buffered output stream: BufferedOutputStream
Bufferedoutputstream BOS = new bufferedoutputstream (New fileoutputstream (write file name));
/ / Note: the file here may not exist and will be automatically created later
How to write data: BOS write()
1. Write one byte at a time
2. Write one byte array at a time
3. Write part of a byte array one at a time

package com.shujia.wyh.day23;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;

public class BufferedOuputStreamDemo1 {
    public static void main(String[] args) throws Exception {
        //BufferedOutputStream(OutputStream out)
        //Create a new buffered output stream to write data to the specified underlying output stream.
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("g.txt"));

        //Write one byte array at a time
        bos.write("big data".getBytes());

        bos.close();

    }
}

        FileOutputStream(String name, boolean append)
Create a file output stream and write to the file with the specified name.
true indicates that data can be written additionally

package com.shujia.wyh.day22;

import java.io.FileOutputStream;

public class FileOutputStreamDemo3 {
    public static void main(String[] args) throws Exception {
        //FileOutputStream(String name, boolean append)
        //Create a file output stream and write to the file with the specified name.
        //true indicates that data can be written additionally
        FileOutputStream fos = new FileOutputStream("e.txt", true);
        fos.write("it's snowing today\r\n".getBytes());
        fos.write("But no snow was seen".getBytes());

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

Character stream (conversion stream) = byte stream + encoding table
A: character input stream Reader
            InputStreamReader:
Inputstreamreader ISR = new inputstreamreader (New FileInputStream);
Simplified writing: FileReader
FileReader fr = new FileReader("file name for reading data");
There are two ways to read data from ordinary character input stream:
1. Read one character at a time
                    int ch = 0;
                    while((ch = fr.read())!=-1){
                        System.out.print((char) ch);
                    }
                    fr.close();
2. Read one character array at a time
                    char[] chars = new char[1024];
                    int length = 0;
                    while((length = fr.read(chars))!=-1){
                        System.out.print(new String(chars,0,length));
                    }
                    fr.close();

package com.shujia.wyh.day23;

import java.io.FileInputStream;
import java.io.InputStreamReader;
public class InputStreamReaderDemo2 {
    public static void main(String[] args) throws Exception {
        //1. Create character input stream object
        FileReader fr = new FileReader("e.txt");

        //Read only one character at a time
        //Character reading, - 1 if the end of the stream has been reached
//        int i = 0;
//        while ((i=fr.read())!=-1){
//            System.out.print((char) i);
//        }

        //Read one character array at a time
        //The number of characters read is - 1 if it has reached the end of the stream
        char[] chars = new char[1024];
        int length = 0;
        while ((length = fr.read(chars)) != -1) {
            System.out.print(new String(chars, 0, length));
        }


        //Release resources
        isr.close();

    }
}

Character buffered input stream: BufferedReader
BufferedReader br = new BufferedReader (New inputstreamreader (New FileInputStream);
BufferedReader br = new BufferedReader(new FileReader("file name for reading data");
The third way to read data from character stream: read one line at a time (readLine())
                String s = null;
                while((s = br.readLine())!=null){
                    System.out.print(s);
                }
                br.close();

package com.shujia.wyh.day23;

import java.io.BufferedReader;
import java.io.FileReader;

public class BufferedReaderDEmo1 {
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new FileReader("a.txt"));
        String s = null;
               while((s = br.readLine())!=null){
                   System.out.print(s);
               }


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

B: character output stream Writer
            OutputStreamWriter:
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("write file name");
Simplified writing method: FileWriter
FileWriter fw = new FileWriter("write file name");

How to write data: FW write()
Write one character at a time
Write an array of characters one at a time
Write a part of a character array one at a time
Write one string at a time
Character buffered output stream: BufferedWriter
BufferedWriter bw = new BufferedWriter(new FileWriter("write file name");

package com.shujia.wyh.day23;

import java.io.BufferedWriter;
import java.io.FileWriter;

public class BufferedWriterDemo1 {
    public static void main(String[] args) throws Exception {
        BufferedWriter bw = new BufferedWriter(new FileWriter("a2.txt"));

        bw.write("hello");
        bw.write("world");

        bw.flush();

        //Release resources
        bw.close();

    }
}

Special data writing method of character stream:
newLine() writes a newline character. The newline character here is different according to different systems.

The difference between flush() and close():
1. After calling flush(), the stream object is not closed and can continue to write data.
2. After calling close(), the flow object is closed, and subsequent data cannot be written through this object.

Example: copy the contents of a.txt in the current project directory to b.txt in the current project directory

package com.shujia.wyh.day23;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;

public class CopyFileDemo4 {
    public static void main(String[] args) throws Exception {
        //1. Create character buffered input stream object and character buffered output stream object
        BufferedReader br = new BufferedReader(new FileReader("a.txt"));
        BufferedWriter bw = new BufferedWriter(new FileWriter("b2.txt"));

        //Read and write one character array at a time
        char[] chars = new char[1024];
        int length = 0;
        while ((length= br.read(chars))!=-1){
            bw.write(chars,0,length); //Direct writing does not require strong conversion
            bw.flush();
        }

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

    }
}

Topics: Java