Android小知识——从SD卡中向APP中读取txt文件的时候出现乱码的问题
2016-05-30 12:00
495 查看
解决:将解析出来的字符串进行编码的处理
private String getShareMessage(String filePath) {
String content = "";
try { // 防止文件建立或读取失败,用catch捕捉错误并打印,也可以throw
/* 读入TXT文件 */
File messageFile = new File(filePath); // 要读取以上路径的input。txt文件
if (!messageFile.exists()) {
return "";
}
InputStreamReader reader = new InputStreamReader(new FileInputStream(messageFile), "utf-8"); // 建立一个输入流对象reader,记住这里的
// unicode
BufferedReader br = new BufferedReader(reader); // 建立一个对象,它把文件内容转成计算机能读懂的语言
String line = "";
while ((line = br.readLine()) != null) { // 一次读入一行数据
content += line;
}
} catch (Exception e) {
Log.e("ggggg", e.toString());
e.printStackTrace();
}
return content;
}
如果这样还是乱码的话 那就看看你写的文件支持什么类型的 然后将utf-8改为你文件所支持的类型
private String getShareMessage(String filePath) {
String content = "";
try { // 防止文件建立或读取失败,用catch捕捉错误并打印,也可以throw
/* 读入TXT文件 */
File messageFile = new File(filePath); // 要读取以上路径的input。txt文件
if (!messageFile.exists()) {
return "";
}
InputStreamReader reader = new InputStreamReader(new FileInputStream(messageFile), "utf-8"); // 建立一个输入流对象reader,记住这里的
// unicode
BufferedReader br = new BufferedReader(reader); // 建立一个对象,它把文件内容转成计算机能读懂的语言
String line = "";
while ((line = br.readLine()) != null) { // 一次读入一行数据
content += line;
}
} catch (Exception e) {
Log.e("ggggg", e.toString());
e.printStackTrace();
}
return content;
}
如果这样还是乱码的话 那就看看你写的文件支持什么类型的 然后将utf-8改为你文件所支持的类型
相关文章推荐
- Lua实现正序和倒序的文件读取方法
- Flex文件读取报错示例
- Android中读取中文字符的文件与文件读取相关介绍
- perl文件读取的几种处理方式小结
- PHP文件读取功能的应用实例
- Python文件读取的3种方法及路径转义
- 解决_CRT_SECURE_NO_WARNINGS 警告
- 资源文件的读取
- 资源文件的读取
- 资源文件读取
- 二分查找
- 快速排序
- 【Unity 笔记】文件的写入与读取(1)
- 【Unity 笔记】文件的写入与读取(2)
- Java开发中文件读取方式总结
- C语言 文件I/O:实现结构体数据 存储到文件和从文件读取
- 异步打开一个文件进行读取
- [IO]——文件的读取
- 读取文件或超大文件的几种方式
- CfileDlg应用