• 从零开始学习 Java:简单易懂的入门指南之IO字符流(三十一)


    1. 字符流

    当使用字节流读取文本文件时,可能会有一个小问题。就是遇到中文字符时,可能不会显示完整的字符,那是因为一个中文字符可能占用多个字节存储。所以Java提供一些字符流类,以字符为单位读写数据,专门用于处理文本文件。

    1.1 字符输入流【Reader】

    java.io.Reader抽象类是表示用于读取字符流的所有类的超类,可以读取字符信息到内存中。它定义了字符输入流的基本共性功能方法。

    • public void close() :关闭此流并释放与此流相关联的任何系统资源。
    • public int read(): 从输入流读取一个字符。
    • public int read(char[] cbuf): 从输入流中读取一些字符,并将它们存储到字符数组 cbuf中 。

    1.2 FileReader类

    java.io.FileReader 类是读取字符文件的便利类。构造时使用系统默认的字符编码和默认字节缓冲区。

    小贴士:

    1. 字符编码:字节与字符的对应规则。Windows系统的中文编码默认是GBK编码表。

      idea中UTF-8

    2. 字节缓冲区:一个字节数组,用来临时存储字节数据。

    构造方法

    • FileReader(File file): 创建一个新的 FileReader ,给定要读取的File对象。
    • FileReader(String fileName): 创建一个新的 FileReader ,给定要读取的文件的名称。

    当你创建一个流对象时,必须传入一个文件路径。类似于FileInputStream 。

    • 构造举例,代码如下:
    public class FileReaderConstructor throws IOException{
        public static void main(String[] args) {
       	 	// 使用File对象创建流对象
            File file = new File("a.txt");
            FileReader fr = new FileReader(file);
          
            // 使用文件名称创建流对象
            FileReader fr = new FileReader("b.txt");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    读取字符数据

    1. 读取字符read方法,每次可以读取一个字符的数据,提升为int类型,读取到文件末尾,返回-1,循环读取,代码使用演示:
    public class FRRead {
        public static void main(String[] args) throws IOException {
          	// 使用文件名称创建流对象
           	FileReader fr = new FileReader("read.txt");
          	// 定义变量,保存数据
            int b ;
            // 循环读取
            while ((b = fr.read())!=-1) {
                System.out.println((char)b);
            }
    		// 关闭资源
            fr.close();
        }
    }
    输出结果:
    我
    是
    程
    序
    员
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    小贴士:虽然读取了一个字符,但是会自动提升为int类型。

    1. 使用字符数组读取read(char[] cbuf),每次读取b的长度个字符到数组中,返回读取到的有效字符个数,读取到末尾时,返回-1 ,代码使用演示:
    public class FRRead {
        public static void main(String[] args) throws IOException {
          	// 使用文件名称创建流对象
           	FileReader fr = new FileReader("read.txt");
          	// 定义变量,保存有效字符个数
            int len ;
            // 定义字符数组,作为装字符数据的容器
             char[] cbuf = new char[2];
            // 循环读取
            while ((len = fr.read(cbuf))!=-1) {
                System.out.println(new String(cbuf));
            }
    		// 关闭资源
            fr.close();
        }
    }
    输出结果:
    我是
    程序
    员序
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    获取有效的字符改进,代码使用演示:

    public class FISRead {
        public static void main(String[] args) throws IOException {
          	// 使用文件名称创建流对象
           	FileReader fr = new FileReader("read.txt");
          	// 定义变量,保存有效字符个数
            int len ;
            // 定义字符数组,作为装字符数据的容器
            char[] cbuf = new char[2];
            // 循环读取
            while ((len = fr.read(cbuf))!=-1) {
                System.out.println(new String(cbuf,0,len));
            }
        	// 关闭资源
            fr.close();
        }
    }
    
    输出结果:
    我是
    程序
    员
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    1.3 字符输出流【Writer】

    java.io.Writer 抽象类是表示用于写出字符流的所有类的超类,将指定的字符信息写出到目的地。它定义了字节输出流的基本共性功能方法。

    • void write(int c) 写入单个字符。
    • void write(char[] cbuf) 写入字符数组。
    • abstract void write(char[] cbuf, int off, int len) 写入字符数组的某一部分,off数组的开始索引,len写的字符个数。
    • void write(String str) 写入字符串。
    • void write(String str, int off, int len) 写入字符串的某一部分,off字符串的开始索引,len写的字符个数。
    • void flush() 刷新该流的缓冲。
    • void close() 关闭此流,但要先刷新它。

    1.4 FileWriter类

    java.io.FileWriter 类是写出字符到文件的便利类。构造时使用系统默认的字符编码和默认字节缓冲区。

    构造方法

    • FileWriter(File file): 创建一个新的 FileWriter,给定要读取的File对象。
    • FileWriter(String fileName): 创建一个新的 FileWriter,给定要读取的文件的名称。

    当你创建一个流对象时,必须传入一个文件路径,类似于FileOutputStream。

    • 构造举例,代码如下:
    public class FileWriterConstructor {
        public static void main(String[] args) throws IOException {
       	 	// 使用File对象创建流对象
            File file = new File("a.txt");
            FileWriter fw = new FileWriter(file);
          
            // 使用文件名称创建流对象
            FileWriter fw = new FileWriter("b.txt");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    基本写出数据

    写出字符write(int b) 方法,每次可以写出一个字符数据,代码使用演示:

    public class FWWrite {
        public static void main(String[] args) throws IOException {
            // 使用文件名称创建流对象
            FileWriter fw = new FileWriter("fw.txt");     
          	// 写出数据
          	fw.write(97); // 写出第1个字符
          	fw.write('b'); // 写出第2个字符
          	fw.write('C'); // 写出第3个字符
          	fw.write(30000); // 写出第4个字符,中文编码表中30000对应一个汉字。
          
          	/*
            【注意】关闭资源时,与FileOutputStream不同。
          	 如果不关闭,数据只是保存到缓冲区,并未保存到文件。
            */
            // fw.close();
        }
    }
    输出结果:
    abC田
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    小贴士:

    1. 虽然参数为int类型四个字节,但是只会保留一个字符的信息写出。
    2. 未调用close方法,数据只是保存到了缓冲区,并未写出到文件中。

    关闭和刷新

    因为内置缓冲区的原因,如果不关闭输出流,无法写出字符到文件中。但是关闭的流对象,是无法继续写出数据的。如果我们既想写出数据,又想继续使用流,就需要flush 方法了。

    • flush :刷新缓冲区,流对象可以继续使用。
    • close :先刷新缓冲区,然后通知系统释放资源。流对象不可以再被使用了。

    代码使用演示:

    public class FWWrite {
        public static void main(String[] args) throws IOException {
            // 使用文件名称创建流对象
            FileWriter fw = new FileWriter("fw.txt");
            // 写出数据,通过flush
            fw.write('刷'); // 写出第1个字符
            fw.flush();
            fw.write('新'); // 继续写出第2个字符,写出成功
            fw.flush();
          
          	// 写出数据,通过close
            fw.write('关'); // 写出第1个字符
            fw.close();
            fw.write('闭'); // 继续写出第2个字符,【报错】java.io.IOException: Stream closed
            fw.close();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    小贴士:即便是flush方法写出了数据,操作的最后还是要调用close方法,释放系统资源。

    写出其他数据

    1. 写出字符数组write(char[] cbuf)write(char[] cbuf, int off, int len) ,每次可以写出字符数组中的数据,用法类似FileOutputStream,代码使用演示:
    public class FWWrite {
        public static void main(String[] args) throws IOException {
            // 使用文件名称创建流对象
            FileWriter fw = new FileWriter("fw.txt");     
          	// 字符串转换为字节数组
          	char[] chars = "我是程序员".toCharArray();
          
          	// 写出字符数组
          	fw.write(chars); // 我是程序员
            
    		// 写出从索引2开始,2个字节。索引2是'程',两个字节,也就是'程序'。
            fw.write(b,2,2); // 程序
          
          	// 关闭资源
            fos.close();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    1. 写出字符串write(String str)write(String str, int off, int len) ,每次可以写出字符串中的数据,更为方便,代码使用演示:
    public class FWWrite {
        public static void main(String[] args) throws IOException {
            // 使用文件名称创建流对象
            FileWriter fw = new FileWriter("fw.txt");     
          	// 字符串
          	String msg = "我是程序员";
          
          	// 写出字符数组
          	fw.write(msg); //我是程序员
          
    		// 写出从索引2开始,2个字节。索引2是'程',两个字节,也就是'程序'。
            fw.write(msg,2,2);	// 程序
          	
            // 关闭资源
            fos.close();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    1. 续写和换行:操作类似于FileOutputStream。
    public class FWWrite {
        public static void main(String[] args) throws IOException {
            // 使用文件名称创建流对象,可以续写数据
            FileWriter fw = new FileWriter("fw.txt"true);     
          	// 写出字符串
            fw.write("我是");
          	// 写出换行
          	fw.write("\r\n");
          	// 写出字符串
      		fw.write("程序员");
          	// 关闭资源
            fw.close();
        }
    }
    输出结果:
    我是
    程序员
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    小贴士:字符流,只能操作文本文件,不能操作图片,视频等非文本文件。

    当我们单纯读或者写文本文件时 使用字符流 其他情况使用字节流

    2. IO异常的处理

    JDK7前处理

    之前的入门练习,我们一直把异常抛出,而实际开发中并不能这样处理,建议使用try...catch...finally 代码块,处理异常部分,代码使用演示:

    public class HandleException1 {
        public static void main(String[] args) {
          	// 声明变量
            FileWriter fw = null;
            try {
                //创建流对象
                fw = new FileWriter("fw.txt");
                // 写出数据
                fw.write("我是程序员"); //我是程序员
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (fw != null) {
                        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

    JDK7的处理

    还可以使用JDK7优化后的try-with-resource 语句,该语句确保了每个资源在语句结束时关闭。所谓的资源(resource)是指在程序完成后,必须关闭的对象。

    格式:

    try (创建流对象语句,如果多个,使用';'隔开) {
    	// 读写数据
    } catch (IOException e) {
    	e.printStackTrace();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    代码使用演示:

    public class HandleException2 {
        public static void main(String[] args) {
          	// 创建流对象
            try ( FileWriter fw = new FileWriter("fw.txt"); ) {
                // 写出数据
                fw.write("我是程序员"); //我是程序员
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    JDK9的改进

    JDK9中try-with-resource 的改进,对于引入对象的方式,支持的更加简洁。被引入的对象,同样可以自动关闭,无需手动close,我们来了解一下格式。

    改进前格式:

    // 被final修饰的对象
    final Resource resource1 = new Resource("resource1");
    // 普通对象
    Resource resource2 = new Resource("resource2");
    // 引入方式:创建新的变量保存
    try (Resource r1 = resource1;
         Resource r2 = resource2) {
         // 使用对象
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    改进后格式:

    // 被final修饰的对象
    final Resource resource1 = new Resource("resource1");
    // 普通对象
    Resource resource2 = new Resource("resource2");
    
    // 引入方式:直接引入
    try (resource1; resource2) {
         // 使用对象
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    改进后,代码使用演示:

    public class TryDemo {
        public static void main(String[] args) throws IOException {
           	// 创建流对象
            final  FileReader fr  = new FileReader("in.txt");
            FileWriter fw = new FileWriter("out.txt");
           	// 引入到try中
            try (fr; fw) {
              	// 定义变量
                int b;
              	// 读取数据
              	while ((b = fr.read())!=-1) {
                	// 写出数据
                	fw.write(b);
              	}
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    3. 综合练习

    练习1:拷贝文件夹

    public class Test01 {
        public static void main(String[] args) throws IOException {
            //拷贝一个文件夹,考虑子文件夹
    
            //1.创建对象表示数据源
            File src = new File("D:\\aaa\\src");
            //2.创建对象表示目的地
            File dest = new File("D:\\aaa\\dest");
    
            //3.调用方法开始拷贝
            copydir(src,dest);
    
    
    
        }
    
        /*
        * 作用:拷贝文件夹
        * 参数一:数据源
        * 参数二:目的地
        *
        * */
        private static void copydir(File src, File dest) throws IOException {
            dest.mkdirs();
            //递归
            //1.进入数据源
            File[] files = src.listFiles();
            //2.遍历数组
            for (File file : files) {
                if(file.isFile()){
                    //3.判断文件,拷贝
                    FileInputStream fis = new FileInputStream(file);
                    FileOutputStream fos = new FileOutputStream(new File(dest,file.getName()));
                    byte[] bytes = new byte[1024];
                    int len;
                    while((len = fis.read(bytes)) != -1){
                        fos.write(bytes,0,len);
                    }
                    fos.close();
                    fis.close();
                }else {
                    //4.判断文件夹,递归
                    copydir(file, new File(dest,file.getName()));
                }
            }
        }
    }
    
    
    • 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

    练习2:文件加密

    public class Test02 {
        public static void main(String[] args) throws IOException {
            /*
                为了保证文件的安全性,就需要对原始文件进行加密存储,再使用的时候再对其进行解密处理。
                加密原理:
                    对原始文件中的每一个字节数据进行更改,然后将更改以后的数据存储到新的文件中。
                解密原理:
                    读取加密之后的文件,按照加密的规则反向操作,变成原始文件。
    
                 ^ : 异或
                     两边相同:false
                     两边不同:true
    
                     0:false
                     1:true
    
                   100:1100100
                   10: 1010
    
                   1100100
                 ^ 0001010
                 __________
                   1101110
                 ^ 0001010
                 __________
                   1100100
    
            */
        }
    
        public static void encryptionAndReduction(File src, File dest) throws IOException {
            FileInputStream fis = new FileInputStream(src);
            FileOutputStream fos = new FileOutputStream(dest);
            int b;
            while ((b = fis.read()) != -1) {
                fos.write(b ^ 2);
            }
            //4.释放资源
            fos.close();
            fis.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
    • 44
    • 45

    练习3:数字排序

    文本文件中有以下的数据:
    2-1-9-4-7-8
    将文件中的数据进行排序,变成以下的数据:
    1-2-4-7-8-9

    实现方式一:

    public class Test03 {
        public static void main(String[] args) throws IOException {
            /*
                文本文件中有以下的数据:
                    2-1-9-4-7-8
                将文件中的数据进行排序,变成以下的数据:
                    1-2-4-7-8-9
            */
    
    
            //1.读取数据
            FileReader fr = new FileReader("myio\\a.txt");
            StringBuilder sb = new StringBuilder();
            int ch;
            while((ch = fr.read()) != -1){
                sb.append((char)ch);
            }
            fr.close();
            System.out.println(sb);
            //2.排序
            String str = sb.toString();
            String[] arrStr = str.split("-");//2-1-9-4-7-8
    
            ArrayList<Integer> list = new ArrayList<>();
            for (String s : arrStr) {
                int i = Integer.parseInt(s);
                list.add(i);
            }
            Collections.sort(list);
            System.out.println(list);
            //3.写出
            FileWriter fw = new FileWriter("myio\\a.txt");
            for (int i = 0; i < list.size(); i++) {
                if(i == list.size() - 1){
                    fw.write(list.get(i) + "");
                }else{
                    fw.write(list.get(i) + "-");
                }
            }
            fw.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

    实现方式二:

    public class Test04 {
        public static void main(String[] args) throws IOException {
            /*
                文本文件中有以下的数据:
                    2-1-9-4-7-8
                将文件中的数据进行排序,变成以下的数据:
                    1-2-4-7-8-9
    
               细节1:
                    文件中的数据不要换行
    
                细节2:
                    bom头
            */
            //1.读取数据
            FileReader fr = new FileReader("myio\\a.txt");
            StringBuilder sb = new StringBuilder();
            int ch;
            while((ch = fr.read()) != -1){
                sb.append((char)ch);
            }
            fr.close();
            System.out.println(sb);
            //2.排序
            Integer[] arr = Arrays.stream(sb.toString()
                                          .split("-"))
                .map(Integer::parseInt)
                .sorted()
                .toArray(Integer[]::new);
            //3.写出
            FileWriter fw = new FileWriter("myio\\a.txt");
            String s = Arrays.toString(arr).replace(", ","-");
            String result = s.substring(1, s.length() - 1);
            fw.write(result);
            fw.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

    后记
    👉👉💕💕美好的一天,到此结束,下次继续努力!欲知后续,请看下回分解,写作不易,感谢大家的支持!! 🌹🌹🌹

  • 相关阅读:
    基于JavaWeb+SpringBoot+Vue房屋租赁系统微信小程序系统的设计和实现
    2022-08-25-RISC-V跃出IoT圈,高性能计算三分天下雏形初现-平头哥先“打样”了SoC原型芯片曳影1520(-JPG)
    UDP通信程序的详细解析
    概念解析 | 心脏电活动和机械活动之间的关系
    基于多目标遗传算法的IEEE14节点系统分布式电源选址定容matlab程序
    Mysql数据丢失分析与数据恢复
    HTML网页规划与设计【冬季奥林匹克运动会——带报告5200字】HTML+CSS+JavaScript
    java毕业设计——基于java+JSP+J2EE的户籍管理系统设计与实现——户籍管理系统
    leetcode *775. 全局倒置与局部倒置(2022.11.16)(逆序对)
    现代数据架构-湖仓一体
  • 原文地址:https://blog.csdn.net/m0_59230408/article/details/132322007