FileReader和BufferedReader的区别

1.FileReader不能一行行读

 1         FileReader fr = null;
 2         try {
 3             fr = new FileReader(new File(path));
 4             StringBuffer stringBuffer = new StringBuffer();
 5             int ch;
 6             while ((ch = fr.read()) != -1) {
 7                 stringBuffer.append(ch);
 8             }
 9             System.out.println(stringBuffer);
10         } catch (Exception e) {
11             e.printStackTrace();
12         } finally {
13             IOUtils.close(fr);//关闭流
14         }

 

2.BufferedReader可以一行行地读

 1         BufferedReader br = null;
 2         try {
 3             br = new BufferedReader(new FileReader(new File(path)));
 4             StringBuffer stringBuffer = new StringBuffer();
 5             String line;
 6             while ((line = br.readLine()) != null) {
 7                 stringBuffer.append(line);
 8           }
 9             System.out.println(stringBuffer);
10         } catch (Exception e) {
11             e.printStackTrace();
12         } finally {
13             IOUtils.close(br);//关闭流
14         }

 

posted @ 2016-07-11 18:13  达哥的博客  阅读(7911)  评论(0编辑  收藏  举报