首页 > 解决方案 > 读取 xml (windows-1252) 文件时出错

问题描述

我在 java 中编写了一个应该读取 xml 文件的程序,但是在使用 windows-1252 读取 xml 文件时出现错误:

java.nio.charset.MalformedInputException:输入长度 = 3

但 UTF-8 对我有用。

public class InputBox {

    public static void XmlOeffnen() throws IOException {


            JFileChooser chooser = new JFileChooser();
            int rueckgabeWert = chooser.showOpenDialog(null);

        String content = null;
        File f = chooser.getSelectedFile();
        String path = f.getAbsolutePath();
        try {
            content = Files.readString(Paths.get(path), Charset.defaultCharset());
        } catch (IOException e) {
            e.printStackTrace();
        }

        Converter.Konvertieren(chooser.getName(), content, path);
    }
}

标签: javaxmlwindows-1252

解决方案


您正在使用默认字符集读取文件。如果要读取具有 Windows-1252 编码的文件,则需要指定该编码。你可以这样做Charset.forName("windows-1252")更新的行应该是这样的:

 content = Files.readString(Paths.get(path), Charset.forName("windows-1252"));

推荐阅读