FileReader和BufferedReader的区别

时间:2023-03-08 16:52:08
FileReader和BufferedReader的区别

1.FileReader不能一行行读

         FileReader fr = null;
try {
fr = new FileReader(new File(path));
StringBuffer stringBuffer = new StringBuffer();
int ch;
while ((ch = fr.read()) != -1) {
stringBuffer.append(ch);
}
System.out.println(stringBuffer);
} catch (Exception e) {
e.printStackTrace();
} finally {
IOUtils.close(fr);//关闭流
}

2.BufferedReader可以一行行地读

         BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(new File(path)));
StringBuffer stringBuffer = new StringBuffer();
String line;
while ((line = br.readLine()) != null) {
stringBuffer.append(line);
          }
System.out.println(stringBuffer);
} catch (Exception e) {
e.printStackTrace();
} finally {
IOUtils.close(br);//关闭流
}