Reader 和 Writer

        在了解 字节流 的读写方式后,我们来看看 字符流 的读写方式

        Reader和Writer是java的IO库提供的的另一个流接口,它们和InputStreasm OutputStream的区别是:前者是字符流,以cahr为单位读写,后者是字节流,以byte单位读取

  • Reader

Reader的常用方法

  • void close() 关闭流并释放与之相关联的所有系统资源

  • String getEncoding() 返回此流使用的字符编码的名称

  • int read() 读一个字符

  • int read(char[] cbuf, int offset, int length) 将字符读入数组的一部分。

  • boolean ready() 告诉这个流是否准备好被读取

 参考代码:

public class TestFileReader {
    public static void main(String[] args) {
        String path = "d:\\a.txt";
        Reader reader = null;
        try {
            reader = new FileReader(path);
            char[] datas = new char[6];
            // 把读取到的2个字符,保存到数组下标从2开始的位置
            reader.read(datas,2,2);
            System.out.println(data);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

         由于Reader是字符流的读取方式,由于不同的统一码,所对应的字符编码也不一样,会出现乱码的问题

         InputStreamReader 用来读取原始字节流,可指定编码格式,因此,我们可以使用InputStreamReder进行解决

参考代码:

public class TestFileReader2 {
    public static void main(String[] args) {
        String path = "d:\\a.txt";
        Reader reader = null;
        try {
            reader = new InputStreamReader(new FileInputStream(path), "utf-8");
            char[] datas = new char[6];
            reader.read(datas);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
  •  BufferedReader

        BufferedReader可以当作FileReader的升级版,自带缓冲区,它可以先把一批数据读到缓冲区,接下来的读操作都是从缓冲区内获取数据,避免每次都从数据源读取数据,进行字符编码转换,从而提高读取操作的效率

        之所以理解为升级版,是除了上述有点之外,还有新增方法:readlLine()为典型

        我们直接上代码

public class TestBufferedReader {
    public static void main(String[] args) {
        String path = "d:\\a.txt";
        // BufferedReader具有子类读题的readline()方法,一般不用父类类型定义
        BufferedReader reader = null;
        try {
            // BufferedReader传入参数需要是一个Reader类或其子类
            reader = new BufferedReader(new FileReader(path));
            while (true) {
                // 若内容读完,readline返回一个null
                String line = reader.readLine();
                if (line == null) {
                    break;
                }
                System.out.println(line);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

         与FileReader一样,我们需要解决乱码问题

public class TestBufferedReader2 {
    public static void main(String[] args) {
        String path = "d:\\a.txt";
        BufferedReader reader = null;
        try {
            // BufferedReader传入参数需要是一个Reader类或其子类
            reader = new BufferedReader(new InputStreamReader(
                                        new FileInputStream(path), "gbk")
            );
            while (true) {
                String line = reader.readLine();
                if (line == null) {
                    break;
                }
                System.out.println(line);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
  • FileWriter

        Writer可以当作自带编码转换器的OutputStream,它把char转换为byte并输出

        用法与Reader一致,我们不做赘述

public class TestFileWriter {
    public static void main(String[] args) {
        String path = "d:\\info.txt";
        Writer writer = null;
        try {
            // 1、创建对象
            writer = new FileWriter(path, true);
            // 2、进行写操作
            String content = "好好学习,天天向上\n";
            writer.write(content);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (writer != null) {
                    writer.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
  • BufferedWriter

        同样,BufferedWriter也自带缓冲区, 可以提高字符流写文本文件的效率

        BufferedWriter与BufferedReader的流方向正好相反,BufferedWriter是把一批数据写到缓冲区,当缓冲区满的时候,再把缓冲区的数据写到字符输出流中,这可以避免每次都执行物理写操作,从而提高io操作的效率

参考代码:

public class TestFileReader {
    public static void main(String[] args) {
        String path = "d:\\a.txt";
        Reader reader = null;
        try {
            reader = new InputStreamReader(new FileInputStream(path), "utf-8");
            char[] datas = new char[6];
            reader.read(datas);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
  •  InputStream/ OutputStream 和 Reader/Writer 的区别

 

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值