【问题标题】:How to read an ansi-coded file with german (european) characters and display correctly如何读取带有德语(欧洲)字符的 ansi 编码文件并正确显示
【发布时间】:2017-04-16 04:06:16
【问题描述】:

我有一个用 Windows 编辑器(或 Word)编写的标准文本文件,并以 ANSI 格式保存在 android 设备中。如果我打开并阅读此文件并将其显示在我的 Android 设备上,则所有字符都会正确显示,但德语变音符号 äÄöÖüÜß 除外。在黑色菱形中显示的是白色问号,而不是这些字符。 (我使用remoteViews.setTextViewText(...将它们显示在主屏幕小部件中)

我用谷歌搜索了几个小时,发现了很多关于使用 UTF-8 编码等的提示。但是当我以 UTF-8 或除 ANSI 以外的任何格式保存文件时,我会遇到异常并且无法读取文件全部。使用 android 编辑器显示文件的编码在 ANSI 和 UTF-8 中都是正确的。

我的程序太长,无法在此处复制,因此我提取了希望相关的部分并将其放在下面。请帮忙!

public class Test {

    static void readFile() {
        File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), "birthday.txt");
        if (file.exists())
        {
            try {
                FileReader fileReader = new FileReader(file);
                BufferedReader br = new BufferedReader(fileReader);
                //BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(directory+"birthday.txt"), "windows-1252"));

                String lineOfText;
                while ((lineOfText = br.readLine()) != null) {
                    //Output lineOfText  via remoteViews.setTextViewText(WidgetOutput.getRef(linecounter).getIdWhat(), lineOfText);

                }
                br.close();

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

}

【问题讨论】:

  • 读取 UTF-8 文件时遇到什么异常?
  • 在这些阅读器的某个地方,您可以添加编码参数。试试看。

标签: java android file encoding ansi


【解决方案1】:

评论的BufferedReader 行看起来应该可以工作。但是,windows-1252java.nio 中类的规范名称。对于java.io(如InputStreamReader)中的类,规范名称是Cp1252。见Supported Encodings

您也不妨试试ISO-8859-1 (nio) 或ISO8859_1 (io)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-30
    • 1970-01-01
    • 2021-05-03
    • 1970-01-01
    • 2014-08-30
    • 2021-10-11
    相关资源
    最近更新 更多