我的资产文件夹中有一个文本文件,其中有一些德语和中文文本。我使用以下代码读取文件:
public String loadJSONFromAsset() {
String json = null;
try {
InputStream is = getActivity().getAssets().open("name.json.txt");
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
json = new String(buffer, "UTF-8");
} catch (IOException ex) {
ex.printStackTrace();
return null;
}
return json;
}
但是,当在我的TextView中显示时,它显示为菱形内的问号。我的代码有什么问题?有人请帮忙。
可能是由于您的文件使用不同的字符集进行编码,例如,使用MS Notepad时默认情况下为ANSI。您可以尝试使用其他字符集以获得预期的结果。
json = new String(buffer, "ISO8859-1");
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句