简体   繁体   English

读取xml(Windows-1252)文件时出错

[英]Error while reading a xml (windows-1252) file

I have written a programm in java that should read xml files, but I have got error while reading a xml file with windows-1252: 我已经用Java编写了一个程序,该程序应该读取xml文件,但是使用Windows-1252读取xml文件时出现错误:

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

but UTF-8 works for me. 但是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);
    }
}

You are reading the file using the default Charset. 您正在使用默认字符集读取文件。 If you want to read a file with Windows-1252 encoding, you need to specify that encoding. 如果要读取Windows-1252编码的文件,则需要指定该编码。 You can do this with Charset.forName("windows-1252") The updated line should look something like: 您可以使用Charset.forName("windows-1252")来完成此操作,更新后的行应类似于:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM