【问题标题】:Reading .txt file with UTF-8使用 UTF-8 读取 .txt 文件
【发布时间】: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


【解决方案1】:

解决方案:

我不得不更改 txt 文件编码:

assets文件夹->我的文件->右键->属性->将编码改为UTF8

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-04
    • 2012-01-05
    • 2016-08-14
    • 2016-07-27
    相关资源
    最近更新 更多