读书人

TextView显示中文乱码,该怎么处理

发布时间: 2013-11-26 22:51:31 作者: rapoo

TextView显示中文乱码
通过BufferedReader从文件中读取了字符串,但是在TextView中显示中文字符的时候发生了乱码,String的编码方式已经改成了“GBK”
[解决办法]

引用:
Quote: 引用:

楼主,注意文件的写入编码方式和读取编码方式要一致啊!推荐用utf-8编码,写入用这个编码,读取也用这个。
你好,我初学android,读取文件是直接从txt文件中读取的

     try {
InputStream in = getResources().getAssets().open("data.txt");
BufferedReader buffer = new BufferedReader(new InputStreamReader(in, "utf-8"));

String pureWord = new String();
String explain = new String();

while((pureWord = buffer.readLine()) != null){
explain = buffer.readLine();
words.add(new Word(pureWord, explain));
}
in.close();
} catch (IOException e) {
e.printStackTrace();
}




这样还是出错了,为什么啊?


你确信data.txt是utf-8编码吗? 用notepad++创建一个包含中文的utf-8编码的文件,再打开试试?

[解决办法]
引用:
Quote: 引用:

楼主,注意文件的写入编码方式和读取编码方式要一致啊!推荐用utf-8编码,写入用这个编码,读取也用这个。
你好,我初学android,读取文件是直接从txt文件中读取的

     try {
InputStream in = getResources().getAssets().open("data.txt");
BufferedReader buffer = new BufferedReader(new InputStreamReader(in, "utf-8"));

String pureWord = new String();
String explain = new String();

while((pureWord = buffer.readLine()) != null){
explain = buffer.readLine();
words.add(new Word(pureWord, explain));
}
in.close();
} catch (IOException e) {
e.printStackTrace();
}




这样还是出错了,为什么啊?


你要确定你的data.txt文件是用utf-8编码写入的?你把这个文件拉出来,用notepad++打开,utf-8编码方式看看是不是乱码。
或者你按楼上说的新创建一个utf-8编码的文件,随便写点内容进去,然后代码读取出来看看。
[解决办法]
编码格式应该没变,查一下源码

读书人网 >Android

热点推荐