当前位置:   article > 正文

Java IO:字节流、字符流、缓冲流分析及案例说明_java字符流和字符缓冲流复制文本的区别

java字符流和字符缓冲流复制文本的区别

1.前言

  java IO类库我一直觉得是一个比较难理解的类库,从我在小白时期学习的时候就觉得比较难。最近又重新学习思考了下,把自己的一些分析和思考分享一下。

2.IO是什么?

  IO在计算机中指Input、Output,即输入和输出,是计算机中应用程序与各种外部设备之间的数据传递,比如磁盘(文件)、网络(网络连接)、键盘等这些设备,其中这里Input和Output的是以计算机的角度来看待的。

3.Java的IO类库

3.1 Java IO类库的基础框架

在这里插入图片描述
  从这个图上门看到,Java IO非常庞大,但是层次上还是比较清晰的,所有的其实都是围绕着两个大的主干(字节流和字符流)来扩展的。
  所谓字节(Byte),是一种存储的计量单位,表示数据量多少,是计算机存储容量的计量单位,一个字节占8位;而字符(Character)是计算机使用的文字和符号,比如’a’,‘中’,'#'等;在ASCII码中,一个英文字母占用一个字节,一个汉字占用两个字节;在UTF-8编码中,一个英文字母占用一个字节,一个汉字占用三个字节,在Unicode编码中,一个英文字符占用一个字节,一个汉字占用两个字节。
  Java中通过流(stream)来处理IO,那什么是流(stream)呢?其实流是一种抽象的概念,通过先进先出的方式,把一连串的数据(字节/字符)发送出去的通道。
在这里插入图片描述
  如上图所示,当程序需要读取数据时,就开启一个从数据源到计算机程序(内存)的管道,将数据传输过去;当程序需要往目标文件写出数据时,就会开启一个从计算机程序(内存)到目标文件的管道,把数据传输过去,写入文件。这些数据就像是水流一样,在管道中按照顺序流动。
  从上面的描述我们可以看到,流基本上有以下几个特点:

  1. 顺序性-先进先出:最先读入输入流的数据最先被计算机程序接收;最先写入到输出流的数据最新被目标文件接收;
  2. 只读或只写:每个流只能是输入流或者输出流,即一个管道要么是输出,要么是输入,不能二者兼得。

3.2 Java IO的分类

  1. 按照数据流向:输入流和输出流
  2. 按照数据单位:字节流和字符流
    在这里插入图片描述
      对于输入流和输出流比较好理解,但为什么有了字节流,还要出现字符流?以UTF-8编码为例,一个英文字母对应一个字节,那么一次读取一个字节是没有问题的,但遇到中文,一个汉字对应三个字节,如果每次读取一个字节,就会把整个汉字给割裂开来,就会出现乱码,所以为了更方便的处理中文、日文、韩文等这些字符,就推出了字符流。
      我们可以看几个例子如下:
 public static void main(String[] args) throws IOException, InterruptedException {
        char a = (char)System.in.read();
        System.out.println(a);
    }
  • 1
  • 2
  • 3
  • 4

我们输入a,会返回 a:
在这里插入图片描述
如果我们输入“中”,会返回乱码:
在这里插入图片描述
这是因为System.in.read()一次只能读取一个字节,其范围是-1~255,(其中-1表示到了结尾),但“中”占用了三个字节,所以会出现乱码。
  字节流和字符流的主要区别:
在这里插入图片描述

3.3 Java IO的基本用法

3.3.1 字节流

3.3.3.1 用字节流写文件
    public static void main(String[] args) throws IOException {
        String s ="你好";
        byte[] bytes = s.getBytes("utf-8");
        File file = new File("D:/data.txt");
        OutputStream os = new FileOutputStream(file);
        os.write(bytes);
        os.close();
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
3.3.3.2 用字节流读文件
    public static void main(String[] args) throws IOException {
        File file = new File("d:/data.txt");
        InputStream is = new FileInputStream(file);
        byte[] bytes = new byte[(int) file.length()];
        is.read(bytes);
        System.out.println(new String(bytes));
        is.close();
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
3.3.3.3 用字节流copy文件
    public static void copyFile(String srcPath, String destPath) throws IOException {
        File src = new File(srcPath);
        File dest = new File(destPath);
        if (!src.isFile()) {
            throw new RuntimeException("拷贝的源文件不是文件类型,请检查源文件");
        }
        InputStream is = new FileInputStream(src);
        OutputStream os = new FileOutputStream(dest);
        //设置一个缓存区,每次读取一个字节,从输入流中一个字节一个字节的,将读取的字节放入这下面这个字节数组中,
        //如果字节数组满了,然后将此字节数组写入到输出流中
        byte[] bufferBytes = new byte[1024];
        int length = 0;
        //如果是-1,说明已经到文件结尾了,就暂停了;
        //如果不是-1,当bufferBytes字节数组满了以后,就将此字节数组写入到输出流中,完成后,继续往bufferBytes字节数组中写入,如此循环
        while ((length = is.read(bufferBytes)) != -1) {
            os.write(bufferBytes, 0, length);
        }
        os.flush();
        os.close();
        is.close();
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

3.3.2 字符流

3.3.2.1 用字符流写文件
    public static void main(String[] args) throws IOException {
        File file = new File("d:/data_copy.txt");
        Writer writer = new FileWriter(file);
        String s = "你好啊你好啊你是谁啊你是老大么?";
        writer.write(s);
        writer.close();
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
3.3.2.2 用字符流读文件
    public static void main(String[] args) throws IOException {
        File file = new File("d:/data.txt");
        Reader reader = new FileReader(file);
        char[] charArray = new char[(int) file.length()];
        int size = reader.read(charArray);
        System.out.println(new String(charArray).toString());
        reader.close();
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
3.3.2.3 用字符流copy文件
   public static void copyFileByChar(String srcPath, String destPath) throws IOException {
        File src = new File(srcPath);
        File dest = new File(destPath);
        if (!src.isFile()) {
            throw new RuntimeException("拷贝的源文件不是文件类型,请检查源文件");
        }
        Reader is = new FileReader(src);
        Writer os = new FileWriter(dest);
        char[] bufferChars = new char[1024];
        int length = 0;
        while ((length = is.read(bufferChars)) != -1) {
            os.write(bufferChars, 0, length);
        }
        os.flush();
        os.close();
        is.close();
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

3.3.3 缓冲流

3.3.3.1 用缓冲字节流copy文件
    public static void copyFile(String srcPath, String destPath) throws IOException {
        File src = new File(srcPath);
        File dest = new File(destPath);
        if (!src.isFile()) {
            throw new RuntimeException("拷贝的源文件不是文件类型,请检查源文件");
        }
        InputStream is = new BufferedInputStream(new FileInputStream(src));
        OutputStream os = new BufferedOutputStream(new FileOutputStream(dest));
        //设置一个缓存区,每次读取一个字节,从输入流中一个字节一个字节的,将读取的字节放入这下面这个字节数组中,如果字节数组满了,然后将此字节数组写入到输出流中
        byte[] bufferBytes = new byte[1024];
        int length = 0;
        //如果是-1,说明已经到文件结尾了,就暂停了;
        //如果不是-1,当bufferBytes字节数组满了以后,就将此字节数组写入到输出流中,完成后,继续往bufferBytes字节数组中写入,如此循环
        while ((length = is.read(bufferBytes)) != -1) {
            os.write(bufferBytes, 0, length);
        }
        os.flush();
        os.close();
        is.close();
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
3.3.3.2 用缓冲字符流copy文件
    public static void copyFileByChar(String srcPath, String destPath) throws IOException {
        File src = new File(srcPath);
        File dest = new File(destPath);
        if (!src.isFile()) {
            throw new RuntimeException("拷贝的源文件不是文件类型,请检查源文件");
        }
        Reader is = new BufferedReader(new FileReader(src));
        Writer os = new BufferedWriter(new FileWriter(dest));
        char[] bufferChars = new char[1024];
        int length = 0;
        while ((length = is.read(bufferChars)) != -1) {
            os.write(bufferChars, 0, length);
        }
        os.flush();
        os.close();
        is.close();
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/469680
推荐阅读
相关标签
  

闽ICP备14008679号