Java——Detailed explanation of IO stream

1. IO relationship structure

Java 's IO stream is the basis for realizing input and output, which can easily realize data input and output operations, and is often associated with file operations. So IO flow is a very important knowledge point

IO streams can be classified according to different angles

According to the flow direction: (centered on the program) input stream output stream

According to the operating unit :: byte stream (any data can be transmitted) character stream (text)

Divided according to function : node flow (operating basic functions, realizing reading and writing, data from data source to destination) function flow (enhanced function, improving performance, operation and node flow)

Below are all relevant streams (we only need to remember a few common ones)

2. Documents

Create a new file and related operations

package IO;

import java.io.File;

/**
 * @title: 创建一个文件以及相关操作
 * @Author kun
 * @Date: 2022/1/5 9:20
 * @Version 1.0
 */
public class IO_01_File {
    public static void main(String[] args) {
        File file = new File("F:\\我的项目\\idea项目\\java\\src\\IO\\IO.txt");
        try{
            file.createNewFile();
            System.out.println("该文件名:"+file.getName());
            System.out.println("该父目录字符串:"+file.getParent());
            System.out.println();
        }catch (Exception e){
            
        }
        
    }
}

3. Byte stream

A byte stream has an input stream and an output stream

Byte input stream: InputStream

Byte output stream: OutputSream

(according to the file created above, and write the information), e.g.

 byte input stream

package IO;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * @title: 读文件(读文件就是读到程序,对于程序来说是写,是输入,所以用InputStream)
 * @Author kun
 * @Date: 2022/1/5 9:38
 * @Version 1.0
 */
public class IO_02_InputStream {
    public static void main(String[] args) {
        //1.建立联系
        File file = new File("F:\\我的项目\\idea项目\\java\\src\\IO\\IO.txt");
        //2.选择流
        FileInputStream fis=null;
        try {
            fis = new FileInputStream(file);
            //3.读
            int num= fis.read();
            System.out.println((char) num);
            int num2= fis.read();
            System.out.println((char) num2);
            int num3= fis.read();
            System.out.println((char) num3);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                fis.close();
            }catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

byte output stream  

package IO;

import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @title: 写文件
 * @Author kun
 * @Date: 2022/1/5 9:49
 * @Version 1.0
 */
public class IO_03_OutputStream {
    public static void main(String[] args) {
        FileOutputStream fos=null;
        try{
            //注意:目标文件如果不存在,系统会自动创建,但是目录不会自动创建
            //1.选择流,  true追加写  false覆盖写
            fos = new FileOutputStream("F:\\我的项目\\idea项目\\java\\src\\IO\\IO.txt", true);
            //2.写(字节)
            byte[] str="java真尼玛好学".getBytes();
            fos.write(str,0,3);
            //3.刷出
            fos.flush();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try{
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
        
    }
}

 Realize file restoration (byte stream file copy)

package IO;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @title: 文件的复制(读与写)
 * @Author kun
 * @Date: 2022/1/5 10:05
 * @Version 1.0
 */
public class IO_04_Copy {
    public static void main(String[] args) {
        //建立联系
        File file1 = new File("F:\\我的项目\\idea项目\\java\\src\\IO\\IO.txt");
        File file2 = new File("F:\\我的项目\\idea项目\\java\\src\\IO\\IO_copy.txt");
        //读文件 写文件
        FileInputStream fis=null;
        FileOutputStream fos=null;
        try{
            fis = new FileInputStream(file1);
            fos = new FileOutputStream(file2);
            //一次读1024个字节
            byte[]bt=new byte[1024];
            //读出的字节长度
            int len=-1;
            //当读出的字节长度不为-1,证明还有字节需要读
            while ((len= fis.read(bt))!=-1){
                fos.write(bt,0,len);
            }
            //读完后刷出
            fos.close();
            //读完后关闭IO流,在finally完成
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try{
                if(fis!=null){
                    fis.close();
                }
                if(fos!=null){
                    fos.close();
                }
            }catch (IOException e){
                e.printStackTrace();
            }
            
        }
        
    }
}

4. Character stream

 Character streams only write plain text

Character input stream: Reader FileReader

Character output stream: Writer FileWriter

character stream input and output

package IO;

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

/**
 * @title: 字符流Reader和Writer
 * @Author kun
 * @Date: 2022/1/5 10:20
 * @Version 1.0
 */
public class IO_05_ReaderAndWriter {
    public static void main(String[] args) {
        FileReader reader=null;
        FileWriter writer=null;
        try{
            reader = new FileReader("F:\\我的项目\\idea项目\\java\\src\\IO\\IO.txt");
            writer =new FileWriter("F:\\我的项目\\idea项目\\java\\src\\IO\\IO_copy2.txt");
            //读 写
            //注意:字节流读的是字节,字符流读的是字符
            char []chars=new char[1024];
            int len=-1;
            while ((len=reader.read(chars))!=-1){
                writer.write(chars,0,len);
            }
            //读完刷出
            writer.flush();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try{
                if(reader!=null){
                    reader.close();
                }
                if(writer!=null){
                    writer.close();
                }
            }catch (IOException e){
                e.printStackTrace();
            }
        }
    }
}

5. Function flow

Function flow acts on node flow

5.1, buffer stream

Buffer stream: improve performance, improve read and write efficiency

Byte-buffered input stream:BufferedInputStream

Byte-buffered output stream:BufferedOutputStream

Character buffered input stream:BufferedReader

Character buffered output stream:BufferedWriter

Both add a buffered in front of the byte stream and the character stream, and the use is the same as the byte stream and the character stream

byte-buffered input stream and byte-buffered output stream

package IO;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @title: 字节缓冲输入流与字节缓冲输出流
 * @Author kun
 * @Date: 2022/1/5 10:35
 * @Version 1.0
 */
public class IO_06_BufferedInputStreamAndBufferedOutputStream {
    public static void main(String[] args) {
        //因为缓冲流都是作用于字节流和字符流之上的,所以我们要先创建字节流或者字符流
        FileInputStream fis=null;
        FileOutputStream fos = null;
        BufferedInputStream bis=null;
        BufferedOutputStream bos=null;
        try{
            fis = new FileInputStream("F:\\我的项目\\idea项目\\java\\src\\IO\\IO.txt");
            fos = new FileOutputStream("F:\\我的项目\\idea项目\\java\\src\\IO\\IO_copy3.txt");
            //缓冲流都是作用于字节流和字符流之上
            bis = new BufferedInputStream(fis);
            bos =new BufferedOutputStream(fos);
            //读 写
            byte []bt=new byte[1024];
            int len=-1;
            while ((len=bis.read(bt))!=-1){
                bos.write(bt,0,len);
            }
            //读写完刷出
            bos.flush();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                if(bis!=null){
                    bis.close();
                }
                if(bos!=null){
                    bos.close();
                }
                if(fis!=null){
                    fis.close();
                }
                if(fos!=null){
                    fos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

Character buffered input stream and character buffered output stream  

package IO;

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

/**
 * @title: 字符缓冲输入流与字符缓冲输出流
 * @Author kun
 * @Date: 2022/1/5 10:52
 * @Version 1.0
 */
public class IO_07_BufferedReaderAndBufferedWriter {
    public static void main(String[] args) {
        //因为缓冲流都是作用于字节流和字符流之上的,所以我们要先创建字节流或者字符流
        FileReader fr=null;
        FileWriter fw=null;
        BufferedReader br=null;
        BufferedWriter bw=null;
        try {
            fr = new FileReader("F:\\我的项目\\idea项目\\java\\src\\IO\\IO.txt");
            fw=new FileWriter("F:\\我的项目\\idea项目\\java\\src\\IO\\IO_copy4.txt");
            br=new BufferedReader(fr);
            bw=new BufferedWriter(fw);
            //读,写  注意是字符流
            char[]chars=new char[1024];
            int len=-1;
            while ((len=br.read(chars))!=-1){
                bw.write(chars,0,len);
            }
            //读写完刷出
            bw.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                if(br!=null){
                    br.close();
                }
                if(bw!=null){
                    bw.close();
                }
                if(fr!=null){
                    fr.close();
                }
                if(fw!=null){
                    fw.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

5.2, conversion stream (less used)

 Byte conversion character input stream:InputStreamReader

Byte conversion character output stream:OutputStreamWriter

Byte to character

package IO;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

/**
 * @title: 字节转字符
 * @Author kun
 * @Date: 2022/1/5 11:02
 * @Version 1.0
 */
public class IO_08_InputStreamReaderAndOutputStreamWriter {
    public static void main(String[] args) {
        FileInputStream fis=null;
        FileOutputStream fos=null;
        InputStreamReader isr=null;
        OutputStreamWriter osw=null;
        try {
            fis = new FileInputStream("F:\\我的项目\\idea项目\\java\\src\\IO\\IO.txt");
            fos=new FileOutputStream("F:\\我的项目\\idea项目\\java\\src\\IO\\IO_copy5.txt");
            isr=new InputStreamReader (fis);
            osw=new OutputStreamWriter(fos);
            char []chars=new char[1024];
            int len=-1;
            while ((len=isr.read(chars))!=-1){
                osw.write(chars,0,len);
            }
            osw.flush();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                if(isr!=null){
                    isr.close();
                }
                if(osw!=null){
                    osw.close();
                }
                if(fis!=null){
                    fis.close();
                }
                if(fos!=null){
                    fos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

5.3. Data flow (less used)

 

Data Flow: (data type)

DataInputStream

DataOutputStream

data flow

package IO;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.Scanner;

/**
 * @title: 数据流(基本类型)
 * @Author kun
 * @Date: 2022/1/5 11:22
 * @Version 1.0
 */
public class IO_09_DataInputStreamAndDataOutputStream {
    public static void main(String[] args) {
        //先从控制台中输入,写入文件,再读取出来
        String str="java真尼玛好学";
        boolean flag=false;
        //将string信息写入文件(对程序来说是写出)
        File file=new File("F:\\我的项目\\idea项目\\java\\src\\IO\\IO_copy6.txt");
        FileOutputStream fos=null;
        FileInputStream fis=null;
        DataOutputStream dos=null;
        DataInputStream dis=null;
        try {
            fos=new FileOutputStream(file);
            dos=new DataOutputStream(fos);
            dos.writeUTF(str);
            dos.writeBoolean(flag);
            //写完刷出
            dos.flush();
            //将文件的信息读出来(对程序来说输入)
            fis=new FileInputStream(file);
            dis=new DataInputStream(fis);
            //注意:一定要和写入的顺序一样
            String new_str =dis.readUTF();
            boolean new_flag= dis.readBoolean();
            System.out.println(new_str);
            System.out.println(new_flag);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Guess you like

Origin blog.csdn.net/m0_60744481/article/details/122321203