Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1703809
  • 博文数量: 171
  • 博客积分: 11553
  • 博客等级: 上将
  • 技术积分: 3986
  • 用 户 组: 普通用户
  • 注册时间: 2006-05-25 20:28
文章分类

全部博文(171)

文章存档

2012年(2)

2011年(70)

2010年(9)

2009年(14)

2008年(76)

分类: Java

2009-03-04 22:41:34

1. /**
   2. * 读取指定的文本文件,并返回内容
   3. *
   4. * @param path 文件路径
   5. * @param charset 文件编码
   6. *
   7. * @return 文件内容
   8. *
   9. * @throws IOException 如果文件不存在、打开失败或读取失败
  10. */

  11. private static String readFile(String path, String charset) throws IOException {
  12. String content = "";
  13. BufferedReader reader = null;
  14. try {
  15. reader = new BufferedReader(new InputStreamReader(new FileInputStream(path), charset));
  16. String line;
  17. while ((line = reader.readLine()) != null) {
  18. content += line + "\n";
  19. }
  20. } finally {
  21. if (reader != null) {
  22. try {
  23. reader.close();
  24. } catch (IOException e) {
  25. // 关闭 Reader 出现的异常一般不需要处理。

  26. }
  27. }
  28. }
  29. return content;
  30. }

阅读(1398) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~