• JavaSE进阶26 - IO流概述、字节流、字符流、转换流、缓冲流


    IO概述

    • 什么是IO?
      • 通过IO可以完成对硬盘文件的读和写。
      • I : Input
      • O : Output
        在这里插入图片描述
    • IO流的分类?
      • 有多种分类方式:
        • 一种方式是按照流的方向进行分类:
          • 以内存作为参照物,
            • 往内存中去,叫做输入(input)。或者叫做读(Read)。
            • 从内存中出来,叫做输出(Output)。或者叫做写(Write)。
        • 另一种方式是按照数据方式不同进行分类:
          • 有的流是按照字节的方式读取数据,一次读取1个字节byte,等同于一次读取8个二进制位。这种流是万能的,什么类型的文件都可以读取。包括:文本文件,图片,声音文件,视频等。
            • 假设文件file1.txt,采用字节流的话是这样读的:
              • a中国bc张三fe
              • 第一次读:一个字节,正好读到’a’(注意:'a’在java中是2个字节,但在windows操作系统中是1个字节)
              • 第二次读:一个字节,正好读到’中’字符的一半(注意:‘中’在java中是2个字节,在windows操作系统中也是2个字节)
              • 第三次读:一个字节,正好读到’中’字符的另外一半。
          • 有的流是按照字符的方式读取数据的,一次读取一个字符,这种流是为了方便读取普通文本文件而存在的,这种流不能读取:图片、声音、视频等文件。只能读取纯文本文件,连word文件都无法读取。
            • 文件file1.txt,采用字符流的话是这样读的:
              • a中国bc张三fe
              • 第一次读:‘a’字符(‘a’字符在windows系统中占用1个字节。)
              • 第二次读:‘中’字符(‘中’字符在windows系统中占用2个字节。)
        • 综述所述,流的分类:
          • 输入流、输出流
          • 字节流、字符流
    • Java中的IO流都已经写好了,我们程序员不需要关心,我们最主要该是掌握在java中已经提供了哪些流,每个流的特点是什么,每个流对象上的常用方法有哪些??
      • java中所有的流都是在:java.io.*下
      • java中主要还是研究:
        • 怎么new流对象?
        • 调用流对象的哪个方法是读,哪个方法是写。
    • Java IO流这块有四大家族:
      • java.io.InputStream 字节输入流
      • java.io.OutputStream 字节输出流
      • java.io.Reader 字符输入流
      • java.io.Writer 字符输出流
      • 四大家族的首领都是抽象类。(abstract class)
      • 所有的流都实现了:
        • java.io.Closeable接口,都是可关闭的,都有close()方法。
        • 流毕竟是一个管道,这个是内存和硬盘之间的通道,用完之后一定要关闭,不然会耗费(占用)很多资源。养成好习惯,用完流一定要关闭。
      • 所有的输出流都实现了:
        • java.io.Flushable接口,都是可刷新的,都有flush()方法。养成一个好习惯,输出流在最终输出之后,一定要记得flush()刷新一下。这个刷新表示将通道/管道当中剩余未输出的数据强行输出完(清空管道!)刷新的作用就是清空管道。
        • 注意:如果没有flush()可能会导致丢失数据。
      • 注意:在java中只要“类名”以Stream结尾的都是字节流。以“Reader/Writer”结尾的都是字符流。
    • java.io包下需要掌握的流有16个:
      • 文件专属:
        • java.io.FileInputStream(掌握)
        • java.io.FileOutputStream(掌握)
        • java.io.FileReader
        • java.io.FileWriter
      • 转换流:(将字节流转换成字符流)
        • java.io.InputStreamReader
        • java.io.OutputStreamWriter
      • 缓冲流专属:
        • java.io.BufferedReader
        • java.io.BufferedWriter
        • java.io.BufferedInputStream
        • java.io.BufferedOutputStream
      • 数据流专属:
        • java.io.DataInputStream
        • java.io.DataOutputStream
      • 标准输出流:
        • java.io.PrintWriter
        • java.io.PrintStream(掌握)
      • 对象专属流:
        • java.io.ObjectInputStream(掌握)
        • java.io.ObjectOutputStream(掌握)

    FileInputStream的使用

    package com.bjpowernode.javase.io;
    
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    
    /*
    java.io.FileInputStream:
        1、文件字节输入流,万能的,任何类型的文件都可以采用这个流来读。
        2、字节的方式,完成输入的操作,完成读的操作(硬盘-->内存)
        3、
     */
    public class FileInputStreamTest01 {
        public static void main(String[] args) {
            FileInputStream fis = null;
            // 创建文件字节流输入对象
            try {
                //FileInputStream fis = new FileInputStream("D:\\Java\\Pros\\IdeaPros\\advanced_21\\test.txt");
                // 写成"/"这样也是可以的
               fis = new FileInputStream("D:/Java/Pros/IdeaPros/advanced_21/test.txt");
    
               // 开始读取
                int readData = fis.read(); // 这个方法返回的是“字节”本身
                System.out.println(readData); // 97 <-- a
    
                readData = fis.read();
                System.out.println(readData); // 98 <-- b
    
                readData = fis.read();
                System.out.println(readData); // 99 <-- c
    
                readData = fis.read();
                System.out.println(readData); // 100 <-- d
    
                readData = fis.read();
                System.out.println(readData); // 101 <-- e
    
                readData = fis.read();
                System.out.println(readData); // 102 <-- f
    
                readData = fis.read();
                System.out.println(readData); // -1 // 读取不到任何数据
    
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                // 在finally语句块中确保流一定关闭。
                if(fis != null){ // 避免空指针异常
                    // 关闭流的前提是:流不是空。流是null的时候没必要关闭。
                    try {
                        fis.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    package com.bjpowernode.javase.io;
    
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    
    /*
    对第一个程序进行改进。循环方式。
    
    分析这个程序的缺点:
        一次读取一个字节byte,这样内存和硬盘交互太频繁,基本上时间/资源都耗费
        在交互上面了。能不能一次读取多个字节呢?可以。
     */
    public class FileInputStreamTest02 {
        public static void main(String[] args) {
            FileInputStream fis = null;
    
            try {
                fis = new FileInputStream("D:/Java/Pros/IdeaPros/advanced_21/test.txt");
                /*while(true){
                    int readData = fis.read();
                    if(readData == -1){
                        break;
                    }
                    System.out.println(readData);
                }*/
    
                // 改造while循环
                int readData = 0;
                while((readData = fis.read()) != -1){
                    System.out.println(readData);
                }
    
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if(fis != null){
                    try {
                        fis.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    package com.bjpowernode.javase.io;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    
    /*
    int read(byte[] b)
        一次最多读取 b.length 个字节
        减少硬盘和内存的交互,提高程序的执行效率。
        往byte[]数组当中读。
     */
    public class FileInputStreamTest03 {
        public static void main(String[] args) {
            FileInputStream fis = null;
            try {
                // 相对路径
                // IDEA默认当前路径:工程Project(区分模块io)的根就是IDEA的默认当前路径
                //fis = new FileInputStream("test.txt");
                //fis = new FileInputStream("io/test2.txt");
                //fis = new FileInputStream("io/src/test3.txt");
                fis = new FileInputStream("io/src/com/bjpowernode/javase/io/test4.txt");
    
                // 开始读,采用byte数组,一次读取多个字节。最多读取“数组.length”个字节。
                byte[] bytes = new byte[4]; // 准备一个4个长度的数组,一次最多读取4个字节。
                // 这个方法的返回值是:读取的数量,不是字节本身。
                int readCount = fis.read(bytes); // 第一次读到了4个字节
                System.out.println(readCount); // 4
                // 将字节数组全部转换成字符串
                // System.out.println(new String(bytes)); // abcd
                // 不应该全部都转换,应该是读取了多少个字节,转换多少个。
                System.out.println(new String(bytes, 0, readCount)); // abcd
    
                readCount = fis.read(bytes); // 第二次只能读取到2个字节
                System.out.println(readCount); // 2
                //System.out.println(new String(bytes)); // efcd
                System.out.println(new String(bytes, 0, readCount)); // ef
    
                readCount = fis.read(bytes); // 1个字节都没有读取到,返回-1
                System.out.println(readCount); // -1
                //System.out.println(new String(bytes)); // efcd
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if(fis != null){
                    try {
                        fis.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    package com.bjpowernode.javase.io;
    
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    
    // 最终版,需要掌握
    public class FileInputStreamTest04 {
        public static void main(String[] args) {
            FileInputStream fis = null;
    
            try {
                fis = new FileInputStream("io/src/com/bjpowernode/javase/io/temp.txt");
                // 准备一个byte数组
                byte[] bytes = new byte[4];
                /*while(true){
                    int readCount = fis.read(bytes);
                    if(readCount == -1) break;
                    // 把byte数组转换成字符串,读取多少转换多少个
                    System.out.print(new String(bytes, 0, readCount));
                }*/
                int readCount = 0;
                while((readCount = fis.read(bytes)) != -1){
                    System.out.print(new String(bytes, 0, readCount));
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally{
                if(fis != null){
                    try {
                        fis.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    package com.bjpowernode.javase.io;
    
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    
    /*
    FileInputStream类的其他常用方法:
        int available(): 返回流当中剩余的没有读到的字节数量。
        long skip(long n): 跳过几个字节不读。
     */
    public class FileInputStreamTest05 {
        public static void main(String[] args) {
            FileInputStream fis = null;
            try {
                fis = new FileInputStream("io/src/com/bjpowernode/javase/io/test4.txt"); // 5
    
                System.out.println("总字节数量:" + fis.available()); // 6
                /*// 读1个字节
                int readByte = fis.read();
                // 还剩下多少可以读的字节数量:5
                System.out.println("剩下多少个字节没有读:" + fis.available());*/
    
                /*// 这个方法有什么用?
                byte[] bytes = new byte[fis.available()]; // 这种方式不太适合太大的文件,因为byte[]数组不能太大。
                // 不需要循环了。
                // 直接读一次就行了。
                int readCount = fis.read(bytes);
                System.out.println(new String(bytes));// abcdef*/
    
    
                // skip,跳过几个字节不读取,这个方法也可能以后会用!
                fis.skip(3);
                System.out.println(fis.read()); // 100 <-- e
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally{
                if(fis != null){
                    try {
                        fis.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49

    FileOutputStream使用

    package com.bjpowernode.javase.io;
    
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    /*
        文件字节输出流,负责写。
        从内存到硬盘。
     */
    public class FileOutputStreamTest01 {
            public static void main(String[] args) {
                FileOutputStream fos = null;
                try {
                    // writeTest.txt文件不存在的时候会自动新建
                    // 这种方式谨慎使用,这种方式会先将原先文件清空,然后重新写入。
                    // fos = new FileOutputStream("writeTest.txt");
    
    
                    // 以追加的方式在文件末尾写入。不会清空原文件内容。
                    fos = new FileOutputStream("io/src/com/bjpowernode/javase/io/temp.txt", true);
                    // 开始写
                    byte[] bytes = {97, 98, 99, 100};
                    // 将byte数组全部写出!
                    fos.write(bytes);
                    // 将byte数组的一部分写出!
                    fos.write(bytes, 0, 2);
    
                    String s = "我是一个中国人, 我骄傲!!!!!!";
                    // 将字符串转换成byte数组
                    byte[] bs = s.getBytes();
                    fos.write(bs);
    
                    // 写完之后,一定记得要刷新!!!!!!!!!!!!!!!!!!!
                    fos.flush();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    if(fos != null){
                        try {
                            fos.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51

    文件复制

    文件的复制原理

    在这里插入图片描述

    代码

    package com.bjpowernode.javase.io;
    
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    /*
    注意目前是拷贝文件,不是拷贝文件夹
    使用FileInputStream + FileOutputStream完成文件的拷贝
    拷贝的过程应该是一边读,一边写。
    使用以上的字节流拷贝文件的时候,文件类型随意,万能的。什么样的文件都能拷贝。
     */
    public class CopyTest01 {
        public static void main(String[] args) {
            FileInputStream fis = null;
            FileOutputStream fos = null;
            try {
                // 创建一个输入流对象
                fis = new FileInputStream("D:\\Java\\Pros\\IdeaPros\\advanced_21\\videoRead\\秒剪辑.mp4");
                // 创建一个输出流对象
                // 这里不加上文件名Copy秒剪辑.mp4,会报错,“拒绝访问”,因为FileOutputStream流访问的是文件,不是文件夹。
                fos = new FileOutputStream("D:\\Java\\Pros\\IdeaPros\\advanced_21\\videoWrite\\Copy秒剪辑.mp4");
    
                // 最核心的:一边读,一边写
                // 8bit(位) = 1Byte(字节)
                // 1024Byte(字节) = 1KB
                // 1024KB = 1MB
                byte[] bytes = new byte[1024 * 1024]; // 1MB(一次最多拷贝1MB)
                int readCount = 0;
                while((readCount = fis.read(bytes)) != -1){
                    fos.write(bytes, 0, readCount);
                }
    
                // 刷新,输出流最后要刷新
                fos.flush();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally{
                // 分开try,不要一起try
                // 一起try,其中一个出现异常,可能会影响另一个流的关闭。
                if(fos != null){
                    try {
                        fos.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if(fis != null){
                    try {
                        fis.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60

    FileReader

    package com.bjpowernode.javase.io;
    
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    
    /*
    FileReader:
        文件字符输入流,只能读取普通文本。
        读取文本内容时,比较方便,快捷。
     */
    public class FileReaderTest {
        public static void main(String[] args) {
            FileReader reader = null;
    
            try {
                // 创建文件字符输入流
                reader = new FileReader("test.txt");
    
                // 准备一个char数组
                char[] chars = new char[4];
                // 往char数组中读
                reader.read(chars); // 按照字符的方式读取:第一次e,第二次f,第三次中,第四次国
                for(char c : chars){
                    System.out.println(c);
                }
    
                /*// 开始读
                char[] chars = new char[4]; // 一次读取4个字符
                int readCount = 0;
                while((readCount = reader.read(chars)) != -1){
                    System.out.print(new String(chars, 0, readCount));
                }*/
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if(reader != null){
                    try {
                        reader.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49

    FileWriter

    package com.bjpowernode.javase.io;
    
    import java.io.FileWriter;
    import java.io.IOException;
    
    /*
    FileWriter:
         文件字符输出流。写。
         只能输出普通文本
     */
    public class FileWriterTest {
        public static void main(String[] args) {
            FileWriter fw = null;
    
            try {
                // 创建文件字符输出流对象
                // fw = new FileWriter("file");
                fw = new FileWriter("file", true);
    
                // 开始写。
                char[] chars = {'我', '是', '中', '国', '人'};
                fw.write(chars);
                fw.write(chars, 2, 3);
                fw.write("我是一名java软件工程师");
                fw.write("\n");
                fw.write("helloworld!");
    
                // 刷新
                fw.flush();
            } catch (IOException e) {
                e.printStackTrace();
            } finally{
                if (fw != null){
                    try {
                        fw.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
    
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43

    复制普通文本文件

    package com.bjpowernode.javase.io;
    
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    
    /*
    使用FileReader FileWriter进行拷贝的话,只能拷贝“普通文本”文件。
    注意:java文件也是普通文本文件,能用记事本编辑的都是普通文本文件,不一定是txt。
     */
    public class CopyTest02 {
        public static void main(String[] args) {
            FileReader in = null;
            FileWriter out = null;
    
            try {
                // 读取
                in = new FileReader("io/src/com/bjpowernode/javase/io/CopyTest02.java");
                // 写入
                out = new FileWriter("CopyTest2.java");
    
                // 一边读一边写
                // 1char = 2Byte
                // 512char = 1024Byte = 1KB
                // 1024KB = 1MB
                char[] chars = new char[1024 * 512]; // 1MB
                int readCount = 0;
                while((readCount = in.read(chars)) != -1){
                    // 读多少,写多少
                    out.write(chars, 0, readCount);
                }
    
                // 刷新
                out.flush();
    
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if(in != null){
                    try {
                        in.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if(out != null){
                    try {
                        out.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58

    BufferedReader

    package com.bjpowernode.javase.io;
    
    import java.io.BufferedReader;
    import java.io.FileReader;
    
    /**
     * BufferedReader:
     *      带有缓冲区的字符输入流
     *      使用这个流的时候不需要自定义char数组,或者说不需要自定义byte数组。自带缓冲。
     */
    public class BufferedReaderTest01 {
        public static void main(String[] args) throws Exception{
            // BufferedReader构造方法的输入参数需要Reader类型的数据
            // 但Reader为抽象类,无法实例化,考虑子类
            // FileReader继承InputStreamReader,并继承于Reader,因此选择传入FileReader的实例
            FileReader reader = new FileReader("CopyTest2.java");
            // 当一个流的构造方法中需要一个流的时候,这个被传进来的流叫做:节点流。
            // 外部负责包装的这个流,叫做:包装流,还有一个名字叫做:处理流。
            // 像当前这个程序来说:FileReader就是一个节点流。BufferedReader就是包装流/处理流。
            BufferedReader br = new BufferedReader(reader);
    
            // 读一行
            /*String firstLine = br.readLine();
            System.out.println(firstLine);
    
            String secondLine = br.readLine();
            System.out.println(secondLine);
    
            String thirdLine = br.readLine();
            System.out.println(thirdLine);*/
    
            // br.readLine()读取一个文本行,但不带换行符。
            String s = null;
            while((s = br.readLine()) != null){
                System.out.println(s);
            }
    
    
            // 关闭流
            // 对于包装流来说,只需要关闭最外层流就行,里面的节点流会自动关闭。(可以看源代码)
            br.close();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43

    节点流和包装流

    package com.bjpowernode.javase.io;
    
    import java.io.BufferedReader;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.InputStreamReader;
    
    /**
     * InputStreamReader
     * 转换流
     */
    public class BufferedReaderTest02 {
        public static void main(String[] args) throws Exception {
            /*
            // 字节流
            FileInputStream in = new FileInputStream("CopyTest2.java");
    
            // 通过转换流转换(InputStreamReader将字节流转换成字符流)
            // in是节点流,reader是包装流
            InputStreamReader reader = new InputStreamReader(in);
    
            // 这个构造方法只能传一个字符流。不能传字节流。
            // reader是节点流,br是包装流
            BufferedReader br = new BufferedReader(reader);
            */
    
            // 合并
            BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("CopyTest2.java")));
    
            String line = null;
            while((line = br.readLine()) != null){
                System.out.println(line);
            }
    
            // 关闭最外层流。
            br.close();
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39

    BufferedWriter

    package com.bjpowernode.javase.io;
    
    import java.io.BufferedWriter;
    import java.io.FileWriter;
    import java.io.IOException;
    
    /*
    BufferedWriter:
        带有缓冲的字符输出流
     */
    public class BufferedWriterTest {
        public static void main(String[] args) throws Exception {
    
            // BufferedWriter out = new BufferedWriter(new FileWriter("copy");
            BufferedWriter out = new BufferedWriter(new FileWriter("copy", true));
    
            out.write("hello world!");
            out.write("\n");
            out.write("hello kitty!");
    
            // 刷新
            out.flush();
            // 关闭最外层流
            out.close();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
  • 相关阅读:
    基于JAVA评标专家管理信息系统计算机毕业设计源码+系统+mysql数据库+lw文档+部署
    自定义Docker镜像--Jupyterlab
    订水商城实战教程10-宫格导航
    Django 请求与相应实例及解决表单返回403问题方法
    开源六轴机械臂myCobot 280末端执行器实用案例解析
    Linux企业应用——Docker(二)之Docker镜像的构建、Dockerfile的编写
    openKylin 0.7 尝鲜
    图形编辑器基于Paper.js教程01:入门必读
    备忘录模式:对象状态的保存与恢复
    深度学习中的函数(一)
  • 原文地址:https://blog.csdn.net/weixin_43636084/article/details/127578572