Chinaunix
标题:
按指定的编码读取文本文件内容
[打印本页]
作者:
jcodeer
时间:
2009-03-04 22:41
标题:
按指定的编码读取文本文件内容
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. }
本文来自ChinaUnix博客,如果查看原文请点:
http://blog.chinaunix.net/u/19742/showart_1851863.html
欢迎光临 Chinaunix (http://bbs.chinaunix.net/)
Powered by Discuz! X3.2