简体   繁体   中英

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.nio.charset.MalformedInputException: Input length = 3

but UTF-8 works for me.

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. You can do this with Charset.forName("windows-1252") The updated line should look something like:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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