作者MattBonner (毁灭性射手)
看板AndroidDev
标题[问题] String 存中文字串
时间Sun Jun 17 16:39:28 2012
各位大大好 不才小弟又来了
目标是用String 宣告一个阵列,然後存入中文
但是用TextView显示存进去的中文都会出现乱码
以下为程式码
String [] answer = new String[50];
try
{
String filePath2 = "/sdcard/answer.dat";
FileReader in2 = new FileReader(filePath2);
StreamTokenizer st2 = new StreamTokenizer(in2);
for(int i=0;i<50;i++)
{
//realize read this line's End or not.
while( st2.nextToken() != StreamTokenizer.TT_EOF)
{
//realize read WORD or not.
if(st2.ttype == StreamTokenizer.TT_WORD)
{
answer[i] = (String)st2.sval;
break;
}
}
}
in2.close();
}//try
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
TextView test =(TextView) findViewById(R.id.textView2);
test.setText(answer[0]);
这样跑下去後显示都会变乱码
如附图
http://ppt.cc/Gl-j
Google发现是UTF-8的问题
但是找不到解法
请问有大大可以提示吗?
感激不尽
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 163.18.104.12
1F:推 hijamoya:FILEREADER 编码设定utf8 06/17 17:42
2F:→ hijamoya:fileReader=new InputStreamReader(fileIn,“utf-8) 06/17 17:42
不才小弟经过楼上大大指点 将程式码改成
FileReader in2 = new FileReader(filePath2);
InputStreamReader isr = new InputStreamReader( new
FileInputStream(filePath2),"utf-8");
StreamTokenizer st2 = new StreamTokenizer(isr);
出来结果还是一样...
请问大大我是写错哪边 还是有什麽误会了
※ 编辑: MattBonner 来自: 163.18.104.12 (06/17 19:35)
不才小弟已经解决
就是在建立dat档就用UTF-8编码存
还是不要自找麻烦好了
谢谢H大了
※ 编辑: MattBonner 来自: 163.18.104.12 (06/17 22:16)