【发布时间】:2015-02-08 12:32:19
【问题描述】:
我想通过按下按钮将我的资产文件夹中的 .txt 文件加载到文本视图中。我这样做了,但我的问题是我的文本文件是 UTF-8 编码文本,一些奇怪的字符被复制到我的 TextView 中,而不是我的真实单词...... 这是我编写的代码和方法,但我不知道应该将“UTF-8”作为参数放在哪里..
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
try {
InputStream iFile = getAssets().open("mytext.txt");
String strFile = inputStreamToString(iFile);
Intent intent=new Intent(MyActivity.this,SecondActivity.class);
intent.putExtra("myExtra", strFile);
final int result=1;
startActivityForResult(intent, result);
} catch (IOException e) {
e.printStackTrace();
}
public String inputStreamToString(InputStream is) throws IOException {
StringBuffer sBuffer = new StringBuffer();
DataInputStream dataIO = new DataInputStream(is);
String strLine = null;
while ((strLine = dataIO.readLine()) != null) {
sBuffer.append(strLine + "\n");
}
dataIO.close();
is.close();
return sBuffer.toString();
}
感谢您的帮助 ;-)
【问题讨论】:
-
1 - 将 UTF-8 定义放入 xml 布局中。 2 - 使用具有该特定语言的所有字形的 font。 3 - 将文本文件保存为 Unicode UTF-8 编码(Android 端不会翻译)。即
don't use Windows Notepad保存文件。 -
尝试在 open() 或 new() 中添加一个额外的参数。
-
InputStreamReader对字符集有重载,请使用该字符集而不是常规的StringBuffer。
标签: java android utf-8 io inputstream