很早之前写过一个小项目,是关于某个音游自制谱面文件内语法内容错误检查的。昨天回看项目时,发现自己在读取文件时使用了 BufferedReader 而注释掉了 InputStreamReader 所以想试试这二者输出的不同。
1.InputStreamReader
处理字符流的抽象类,是字节流通向字符流的桥梁,它将字节流转换为字符流.
2.BufferedReader
由Reader类扩展而来,提供通用的缓冲方式文本读取,readLine读取一个文本行,从字符输入流中读取文本,缓冲各个字符,从而提供字符、数组和行的高效读取。
目标文件:
BufferedReader
public void input() throws IOException {
// maidataContent = "";
File txtfile = new File("D:\\Maidata.txt");
// InputStreamReader read = new InputStreamReader(new FileInputStream(txtfile), "UTF-8");
BufferedReader br = new BufferedReader(new FileReader(txtfile));
String txtLine = null;
while((txtLine = br.readLine())!=null){//使用readLine方法,一次读一行
// TXTString.add(txtLine);
System.out.println(txtLine);
break:
}
}
输出:&title=Caterpillar Song
InputStreamReader
public void input() throws IOException {
// maidataContent = "";
File txtfile = new File("D:\\Maidata.txt");
InputStreamReader read = new InputStreamReader(new FileInputStream(txtfile), "UTF-8");
// BufferedReader br = new BufferedReader(new FileReader(txtfile));
String txtLine = null;
int i;
while((i = read.read() )!=-1){//使用readLine方法,一次读一行
// TXTString.add(txtLine);
System.out.println(i);
break;
}
}
输出:&
由此可见,
InputStreamReader输出单个字符
BufferedReader输出一行字符串