Android - 如何读取 JAVA 中的 txt 文件

Android - How to read txt file in JAVA

请有人帮助我,我正在尝试阅读一个阿拉伯语 .txt 文件,它总是给我这样的阿拉伯语单词“????????”

private String ReadArabic() {

    String words="";
    try {
        InputStream stream = getAssets().open("arabwords.txt");

        int size = stream.available();
        byte[] buffer = new byte[size];
        stream.read(buffer);
        stream.close();
        words = new String(buffer, "UTF-8");
        words = words.replaceAll("(\r|\n)", "");
    } catch (IOException e) {
        // Handle exceptions here
    }
    return words;
}

Its possible that your console is not set to properly display UTF-8 characters.

取决于配置 在 IDE 上。您可能需要在那里配置相同的内容。

尝试使用:"cp864" 而不是 "UTF-8"

它通过使用 "cp1256" 起作用 谢谢大家

private String ReadArabic() {

    String words="";
    try {
        InputStream stream = getAssets().open("arabwords.txt");

        int size = stream.available();
        byte[] buffer = new byte[size];
        stream.read(buffer);
        stream.close();
        words = new String(buffer, "cp1256");
        words = words.replaceAll("(\r|\n)", "");
    } catch (IOException e) {
        // Handle exceptions here
    }
    return words;
}