Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
703 views
in Technique[技术] by (71.8m points)

io - java.nio.charset.MalformedInputException: Input length = 1

I have this (stripped the HTML tags for the code example) function that builds a HTML table out of a CSV, but I get a runtime error everytime I try to run it and I don't know why. Google says that maybe something with the encoding is wrong but I have no idea how to change that.

My CSV is encoded in ANSI and contains characters like ?, ?, ü, ? but I have no control over the encoding or if it will change in the future.

The error occurs here:

Caused by: java.io.UncheckedIOException: java.nio.charset.MalformedInputException: Input length = 1
at java.io.BufferedReader$1.hasNext(Unknown Source)
at java.util.Iterator.forEachRemaining(Unknown Source)
at java.util.Spliterators$IteratorSpliterator.forEachRemaining(Unknown Source)
at java.util.stream.ReferencePipeline$Head.forEach(Unknown Source)
at testgui.Csv2Html.start(Csv2Html.java:121)

Line 121 is

lines.forEach(line -> {

Sourcecode:

protected void start() throws Exception {

    Path path = Paths.get(inputFile);

    FileOutputStream fos = new FileOutputStream(outputFile, true);
    PrintStream ps = new PrintStream(fos);      

    boolean withTableHeader = (inputFile.length() != 0);
    try  {
        Stream<String> lines = Files.lines(path);
        lines.forEach(line -> {
            try {
                String[] columns = line.split(";");
                for (int i=0; i<columns.length; i++) {
                    columns[i] = escapeHTMLChars(columns[i]);
                }       
                if (withTableHeader == true && firstLine == true) {
                    tableHeader(ps, columns);
                    firstLine = false;
                } else {
                    tableRow(ps, columns);
                }


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

            }
        });

    } finally {
        ps.close();
    }

}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can try to utilize the correct encoding by using the Files.lines(Path path, Charset charset) form of the lines method (javadocs).

Here's a list of supported encodings (for the Oracle JVM anyhow). This post indicates that "Cp1252" is Windows ANSI.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...