码迷,mamicode.com
首页 > 其他好文 > 详细

FileReader和BufferedReader的区别

时间:2016-07-11 18:57:40      阅读:191      评论:0      收藏:0      [点我收藏+]

标签:

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         }

 

FileReader和BufferedReader的区别

标签:

原文地址:http://www.cnblogs.com/johnsonwei/p/5661241.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!