【发布时间】:2015-10-02 13:02:43
【问题描述】:
我有一个 txt 文件,其中包含 UTF-8 编码的单词。我把它放在“资产”文件夹中,这是我阅读它们的一种方式:
private void readWordsFromFile() {
AssetManager assetManager = context.getAssets();
try {
InputStream is = assetManager.open("words.txt");
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF8"));
String line;
while ((line = reader.readLine()) != null) {
HashMap<String, String> tempMap = new HashMap<String, String>();
tempMap.put("question", line);
line = reader.readLine();
tempMap.put("answer", line);
words.add(tempMap); // adding to an ArrayList
}
reader.close();
} catch (IOException e) {
Toast toast = Toast.makeText(context, "Error during openin learning file", Toast.LENGTH_SHORT);
toast.show();
}
}
然后我将这些单词从数组中取出,并将它们放入 TextViews 中。布局 xml 文件已指定 UFT8 编码:
<?xml version="1.0" encoding="utf-8"?>
但仍然显示错误。我做错了什么?
【问题讨论】:
-
“但仍然显示错误”——请更详细地解释您的确切症状。
-
这可能是字体问题。
-
The layout xml file has specified UFT8 encoding:。这无关紧要,因为它指定了 xml 文件的编码。不是其中声明的 TextView 的行为。 -
a txt file that contains words with UTF-8 encoding.。不。如果它是一个 utf-8 文本文件,那么所有单词都在该编码中。 -
你看到多少块是错误的字符?一两个?
标签: android utf-8 inputstream