Chinaunix首页 | 论坛 | 博客
  • 博客访问: 141539
  • 博文数量: 19
  • 博客积分: 230
  • 博客等级: 二等列兵
  • 技术积分: 239
  • 用 户 组: 普通用户
  • 注册时间: 2012-03-01 12:47
文章分类
文章存档

2014年(5)

2013年(4)

2012年(10)

分类: Java

2012-03-17 00:44:53

Java中读取文件的内容,直接上代码:

点击(此处)折叠或打开

  1. public static String readContent(File file){
  2.         StringBuffer strBuf = new StringBuffer();
  3.         String content = new String();
  4.         try {
  5.             BufferedReader bufRea = new BufferedReader(new FileReader(file));
  6.             String s = bufRea.readLine();
  7.             while(s!=null){
  8.                 strBuf.append(s);
  9.                 s = bufRea.readLine();
  10.             }
  11.             content = strBuf.toString().replaceAll("\\s*", "");
  12.         } catch (FileNotFoundException e) {
  13.             logger.error("ParseJson==>readContent(File file)----文件没找到---",e.getCause());
  14.             e.printStackTrace();
  15.         } catch (IOException e) {
  16.             logger.error("ParseJson==>readContent(File file)----I/O输入输出流错误!!!----",e.getCause());
  17.             e.printStackTrace();
  18.         }
  19.         return content;
  20.     }
实现的功能就是将文件中的内容读出来,用字符串存储.
可以修改使用其它的输入输出流,看需求了.BufferReader可以方便的按行读取.
ps:就写这些吧,写了两遍 ,为什么创建新分类必须要刷新下才能显示 ,郁闷,我刷新下,结果代码和下边的文字都没了,下次可要吸取教训了

阅读(1587) | 评论(0) | 转发(0) |
0

上一篇:首篇

下一篇:解析json字符串

给主人留下些什么吧!~~