1.java.io.Reader和java.io.InputStream的区别

InputStream Reader
字节流,以byte为单位 字符流,以char为单位
读取字节(-1,0-255):int read() 读取字符,(-1,0-65535):int read()
读到字节数组:int read(byte[] b) 读到字符数组:int read(char[] c)
int read(byte[] b, int off, int len) int read(char[] c, int off, int len)

2.Reader

java.io.Reader是所有字符输入流的超类:

  • int read():

    * 读取下一个字节,并返回字符的int值 (0-65535)

    * 如果已读取到末尾,返回-1

    * read()方法是阻塞(blocking)的,必须等待read()方法返回才能执行下一行代码
  • int read(char[] c):读取若干字符并填充到char[]数组,返回读取的字符数
  • int read(char[] c, int off, int len):指定char[]数组的偏移量和最大填充数
  • void close():关闭Reader

2.1完整的读取一个Reader的所有字符:

方法1:手动关闭

方法2:使用try...finally

方法3:使用try(resource)自动关闭

public class Main {
public static void main(String[] args) throws IOException,ClassNotFoundException {
String file = "./src/main/java/com/testList/Person.txt";
readFile1(file);
System.out.println();
readFile2(file);
System.out.println();
readFile3(file);
}
static void readFile1(String file) throws IOException{
//手动关闭
Reader reader = new FileReader(file);
int n;
while((n=reader.read())!=-1){
System.out.print((char)n);
}
reader.close();
}
static void readFile2(String file) throws IOException{
//使用finally关闭
Reader reader = null;
try{
reader = new FileReader(file);
int n;
while((n=reader.read())!=-1){
System.out.print((char)n);
}
}finally {
if (reader!=null){
reader.close();
}
}
}
static void readFile3(String file) throws IOException{
//自动关闭
try(Reader reader = new FileReader(file)){
int n;
while((n=reader.read())!=-1){
System.out.print((char)n);
}
}
}
}


### 2.2利用缓冲区一次读取多个字符
read()单个返回char,利用竹筒则返回竹筒的长度
```#java
public static void main(String[] args) throws IOException,ClassNotFoundException {
String file = "./src/main/java/com/testList/Person.txt";
try(Reader reader = new FileReader(file)){
char[] buffer = new char[10];
int n;
while((n=reader.read(buffer))!= -1){
System.out.println(n);
}
}
}
```

### 2.3CharArrayReader可以在内存中模拟一个Reader
```#java
public static void main(String[] args) throws IOException,ClassNotFoundException {
char[] data = {'H','e','l','l','o'};
try(Reader reader = new CharArrayReader(data)){
int n;
while((n=reader.read())!=-1){
System.out.println((char)n);
}
}
}
```
## 3.Reader和InputSteam的关系
Reader实际上是基于InputStream构造的:
* FileReader内部持有一个FileInputStream
* Reader可以通过InputStream构造
```#java
InputStream input = new FileInputStreamReader(filename);
Reader reader = new InputStreamReader(input, "UTF-8");
...
reader.close();//当reader执行close时,会调用input的close方法关闭流。因此无需再针对input执行close方法
```
示例
```#java
public static void main(String[] args) throws IOException,ClassNotFoundException {
String file = "./src/main/java/com/testList/Person.txt";
try(Reader reader = new InputStreamReader(
new FileInputStream(file),"UTF-8")){
int n;
while((n=reader.read())!=-1){
System.out.print((char)n);
}
}
}
```

4.总结:

  • Reader定义了所有字符输入流的超类
  • FileReader实现了文件字符流输入
  • CharArrayReader在内容中模拟一个字符流输入
  • Reader是基于InputStream构造的:

    * FileReader使用系统默认编码,无法指定编码

    * 可以通过InputStreamReader指定编码
  • 使用try(resource)保证Reader正确关闭

最新文章

  1. POI完美解析Excel数据到对象集合中(可用于将EXCEL数据导入到数据库)
  2. nginx 软连接
  3. jquery固定在顶部的导航菜单
  4. 在c#程式中配置log4net
  5. c# nullable类型有什么用
  6. Mysql增删改
  7. MATLAB信号与系统分析(五)——连续时间信号的频谱分析
  8. HTML,javaScript,DOM详解
  9. 李洪强iOS开发之keychain的使用
  10. 更改Android AVD路径
  11. 十九、mysql 数据分布式
  12. Dubbo架构设计详解--转载
  13. void (*f(int, void (*)(int)))(int) 函数解析
  14. jQuery中 $ 符号的冲突问题
  15. pomelo
  16. linux 终端相关
  17. shell编程—简介(一)
  18. Node+GitLab实现小程序CI系统
  19. [leetcode] 329. Longest Increasing Path in a Matrix My Submissions Question
  20. LibreOJ #6014. 「网络流 24 题」最长 k 可重区间集

热门文章

  1. Java-Mail邮件开发
  2. weex playGround 扫码空白问题
  3. python 利用selectors实现异步I/O
  4. linux是什么,有什么特点
  5. VS2015在win10上编译的程序不能在Win7上运行的原因
  6. sass compass config.rb
  7. Win10系列:C#应用控件基础13
  8. Spring Boot + Spring Cloud 实现权限管理系统 (系统服务监控)
  9. 厨娘ui设计文档
  10. 剑指Offer 21. 栈的压入、弹出序列 (栈)