Examples of IO stream classification

1, FileInputStream

method

explain

int available()

Returns an estimate of the number of bytes that can be read (or skipped) from the input stream without blocking, which may be 0, or 0 when the end of the stream is detected

int read()

Write a character to the stream

int read​(byte[] b)

Read multiple bytes into b array

int read​(byte[] b, int off, int len)

Read multiple bytes from the stream and fill them in the specified position in the array

void reset()

Reset flow

log skip​(long n)

Skip multiple characters

void close()

Close flow

public class TestFile {
    public static void main(String[] args) {
        //Read temp Txt file content
        //The stream must be closed after use!!! Write in finally
        FileInputStream fis = null;
        try {
            fis = new FileInputStream("D:\\Lession\\Java2113\\temp.txt");
            int temp = 0;
            while((temp = fis.read())!=-1) {
                System.out.println(temp);
            }
        } catch (FileNotFoundException e) {

            e.printStackTrace();
        } catch (IOException e) {

            e.printStackTrace();
        } finally {
            try {
                fis.close();
            } catch (IOException e) {

                e.printStackTrace();
            }
        }
    }
}

Read (byte [] b): read the content into byte [] and the result is the number of bytes read. If no bytes are read, return - 1

public static void main(String[] args) {
        //Which file to read
        FileInputStream fis = null;
        /*File file = new File("D:\\Lession\\Java2113\\temp.txt");
        try {
            fis = new FileInputStream(file);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }*/
        try {
            fis = new FileInputStream("D:\\Lession\\Java2113\\temp.txt");
            //Read a byte and return the unicode value of the byte
            //fis.read();
            //Read into byte [] and return the read length
            //fis.read(byte[] b);
            //Read into byte [] and specify the position of byte array
            //fis.read(byte[] b, int off, int len)
            /* System.out.println((char)fis.read());
            System.out.println(fis.read());
            System.out.println(fis.read());
            System.out.println(fis.read());
            System.out.println(fis.read()); //-1
            */ /*int temp = 0;
            while((temp=fis.read())!=-1) {
                System.out.println((char)temp);
            }*/
            //buffer
            byte[] bytes = new byte[10];
            int temp = 0;
            while((temp=fis.read(bytes))!=-1) {
                for(int i = 0;i<temp;i++) {
                    System.out.println((char)bytes[i]);
                }
            }
        } catch (FileNotFoundException e) {

            e.printStackTrace();
        } catch (IOException e) {

            e.printStackTrace();
        } finally {
            try {
                fis.close();
            } catch (IOException e) {

                e.printStackTrace();
            }
        }
    }

Buffer: as in life, moving books from one classroom to another will be slow and tiring if you move them one by one, such as the use of int read() method;

If a schoolbag is used as a carrying tool, it will be faster to carry the books in the classroom into the schoolbag. There is a buffering process of loading books into the schoolbag, for example, the use of int read (byte [] b) method

Read Chinese

public static void main(String[] args) {
        FileInputStream fis = null;
        try {
            fis = new FileInputStream("D:\\Lession\\Java2113\\temp.txt");
            byte[] bytes = new byte[fis.available()];
            int temp = 0;
            if((temp=fis.read(bytes))!=-1) {
                System.out.println(new String(bytes,"UTF-8"));
            }
        } catch (FileNotFoundException e) {

            e.printStackTrace();
        } catch (IOException e) {

            e.printStackTrace();
        } finally {
            try {
                fis.close();
            } catch (IOException e) {

                e.printStackTrace();
            }
        }
    }

2, FileOutputStream

method

explain

void write​(byte b)

Write a byte to the stream

void write​(byte[] str)

Write a byte array to the stream

void write​(byte[] b, int off, int len)

Writes a portion of a byte array to a stream

void flush()

Refresh stream

void close()

Close flow

Constructor:

FileOutputStream(File file) adds new content from the beginning by default, overwriting.

Fileoutputstream (file, Boolean append) appends content from the end

public class TestFileOutputStream {
    public static void main(String[] args) {
        //To temp Write abcd in TXT
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream("D:\\Lession\\Java2113\\temp.txt");
            fos.write("Abel measured".getBytes());
        } catch (FileNotFoundException e) {

            e.printStackTrace();
        } catch (IOException e) {

            e.printStackTrace();
        }
    }
}

Apply: copy one file to another

 public static void copy1(String srcPath,String destPath) {
        FileInputStream fis = null;
        FileOutputStream fos = null;
        byte[] bytes = new byte[1024];
        try {
            fis = new FileInputStream(srcPath);
            fos = new FileOutputStream(destPath);
            int temp = 0;
            while ((temp = fis.read(bytes)) != -1) {
                fos.write(bytes, 0, temp);
            }
        } catch (FileNotFoundException e) {

            e.printStackTrace();
        } catch (IOException e) {

            e.printStackTrace();
        } finally {
            try {
                fis.close();
            } catch (IOException e) {

                e.printStackTrace();
            }
            try {
                fos.close();
            } catch (IOException e) {

                e.printStackTrace();
            }
        }
    }

3, BufferedInputStream/BufferedOutputStream

• this stream is a processing stream and a byte output stream with buffer.
• BufferedOutputStream can reduce IO times.
public static void copy2(String srcPath,String destPath) {
        FileInputStream fis = null;
        FileOutputStream fos = null;
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {
            fis = new FileInputStream(srcPath);
            fos = new FileOutputStream(destPath);
            bis = new BufferedInputStream(fis);
            bos = new BufferedOutputStream(fos,1024);
            int temp = 0;
            while((temp=bis.read())!=-1) {
                //By default, the buffer stream is output to the device when the buffer is full
                bos.write(temp);
            }
            //Manual flushing buffer output to equipment
            bos.flush();//Scour and read the last part less than 1024
        } catch (FileNotFoundException e) {

            e.printStackTrace();
        } catch (IOException e) {

            e.printStackTrace();
        }finally {
            try {
                bis.close();
            } catch (IOException e1) {

                e1.printStackTrace();
            }
            try {
                bos.close();
            } catch (IOException e1) {

                e1.printStackTrace();
            }
            try {
                fis.close();
            } catch (IOException e) {

                e.printStackTrace();
            }
            try {
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

Finally, you need to flush!!!

4, FileReader and FileWriter

Subclasses of Reader and Writer are character streams

public class TestFileReader {
    public static void main(String[] args) {
        FileReader fr = null;
        try {
            fr = new FileReader("D:\\Lession\\Java2113\\temp1.txt");
            int temp = 0;
            char[] chars = new char[2];
            while((temp=fr.read(chars))!=-1) {
                for(char c:chars) {
                    System.out.println(c);
                }
            }
        } catch (FileNotFoundException e) {

            e.printStackTrace();
        } catch (IOException e) {

            e.printStackTrace();
        }
    }
}
public class TestFileWriter {
    public static void main(String[] args) {
    try {
        FileWriter fw = new FileWriter("D:\\Lession\\Java2113\\temp1.txt");
        fw.write("Hello China");
        fw.flush();
    } catch (IOException e) {

        e.printStackTrace();
        }
    }
}

5, BufferedReader and BufferedWriter

BufferedReader provides the function of reading by line

BufferedWriter provides the ability to customize the buffer size

FileReader fr = null;
    try {
        fr = new FileReader("D:\\Lession\\Java2113\\temp1.txt");
        BufferedReader br = new BufferedReader(fr);
        String str = null;
        while((str=br.readLine())!=null) {
            System.out.println(str);
        }
    } catch (FileNotFoundException e) {

        e.printStackTrace();
    } catch (IOException e) {

        e.printStackTrace();
    }
        
    try {
        FileWriter fw = new FileWriter("D:\\Lession\\Java2113\\temp1.txt");
        BufferedWriter br = new BufferedWriter(fw,1024);
        br.write("Hello, Beijing");
        br.flush();
    } catch (IOException e) {

        e.printStackTrace();
        }

6, InputStreamReader and OutputStreamWriter (transform stream)

Both character streams are inherited from Reader and Writer, which is a bridge for bytes to flow into character stream

public static void main(String[] args) {
        try {
            FileInputStream fis = new FileInputStream("D:\\Lession\\Java2113\\temp.txt");
            InputStreamReader reader = new InputStreamReader(fis,"UTF-8");
            int temp = 0;
            while((temp=reader.read())!=-1) {
                System.out.println((char)temp);
            }
        } catch (FileNotFoundException e) {

            e.printStackTrace();
        } catch (IOException e) {

            e.printStackTrace();
        }
    }
public static void main(String[] args) {
        try {
            FileOutputStream fos = new FileOutputStream("D:\\Lession\\Java2113\\temp1.txt");
            /*fos.write("Hello, Beijing ". getBytes());*/
            OutputStreamWriter osw = new OutputStreamWriter(fos);
            osw.write("Hello, Beijing");
            osw.flush();
        } catch (FileNotFoundException e) {

            e.printStackTrace();
        } catch (IOException e) {

            e.printStackTrace();
        }
    }

7, PrintWriter

public static void main(String[] args) {
        PrintWriter pw = null;
        try {
            FileOutputStream fos = new FileOutputStream("D:\\Lession\\Java2113\\temp1.txt");
            pw = new PrintWriter(fos,true);
            pw.println("Hello, Beijing");
            pw.println("Hello, Winter Olympics");
        } catch (FileNotFoundException e) {

            e.printStackTrace();
        }
    }

8, DataInputStream and DataOutputStream

Byte stream for reading and writing data (basic type data)

try {
    FileOutputStream fos = new FileOutputStream("D:\\Lession\\Java2113\\temp1.txt");
    DataOutputStream dos = new DataOutputStream(fos);
    dos.writeInt(1);
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
public static void main(String[] args) {
    try {
        FileInputStream fis = new FileInputStream("D:\\Lession\\Java2113\\temp1.txt");
        DataInputStream dis = new DataInputStream(fis);
        System.out.println(dis.readInt());
    } catch (IOException e) {

        e.printStackTrace();
    }
}

Keywords: Java Back-end

Added by treppers on Fri, 11 Feb 2022 17:48:36 +0200