当前位置: 代码迷 >> Android >> TextView显示中文乱码,该怎么处理
  详细解决方案

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

热度:27   发布时间:2016-04-28 07:16:53.0
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编码的文件,随便写点内容进去,然后代码读取出来看看。
------解决方案--------------------
编码格式应该没变,查一下源码
  相关解决方案