没有合适的资源?快使用搜索试试~ 我知道了~
安卓读取文本文件并处理中文乱码
5星 · 超过95%的资源 需积分: 50 71 下载量 114 浏览量
2013-04-05
16:56:32
上传
评论
收藏 16KB DOCX 举报
温馨提示
试读
3页
本资料展示了安卓读取文本文件内容,并处理中文乱码的源代码,希望给需求者参考。
资源推荐
资源详情
资源评论
1. 读取文本文件的方法
2. //-------把文本文件读入 String, 字符串之间的分隔符为 '\------
3. //----FilePath 为文件全路径-------------
4. public String ReadTxtFileToString(String FilePath )
5. {
6. String content = ""; //文件内容字符串
7.
8. //获取文件编码
9. String codeformat = getCodeFormat( FilePath ).trim();
10. if ( codeformat.equals("") ) {
11. codeformat = "GBK";
12. }
13.
14. //打开文件
15. try {
16. File file = new File( FilePath );
17. FileInputStream fis = new FileInputStream( file );
18. BufferedReader reader = new BufferedReader(new
InputStreamReader(fis, codeformat));
19. String line;
20. //分行读取
21. while ( ( line = reader.readLine()) != null ) {
22. content += line + "";
23. }
24. fis.close();
25. reader.close();
26. } catch ( java.io.FileNotFoundException e ) {
27. Log.i( tag, FilePath + " 不存在。");
28. } catch (IOException e) {
29. Log.i( tag, "FileOperate 271 error= " +
e.getMessage() );
30. }
31. return content;
32. }
2.判断文本文件的编码格式--------------
public String getCodeFormat(String filepath) {
String codeformat = "";
File file = new File(filepath);
BufferedReader reader;
String text = "";
byte[] first3bytes = new byte[3];
FileInputStream fis = null;
BufferedInputStream in = null;
rbc168
- 粉丝: 1
- 资源: 67
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
前往页