java.nio.charset.MalformedInputException:输入长度= 1 [英] java.nio.charset.MalformedInputException: Input length = 1

查看:267
本文介绍了java.nio.charset.MalformedInputException:输入长度= 1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个(剥离代码示例的HTML标签)函数,用CSV构建HTML表格,但每次尝试运行它时都会遇到运行时错误,我不知道为什么。谷歌表示可能是编码错误,但我不知道如何更改。

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.

我的CSV是用ANSI编码的,包含ä,Ä,Ü等字符,Ö但我无法控制编码或将来是否会发生变化。

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.

此处发生错误:

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)

121行是

lines.forEach(line -> {

源代码:

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();
    }

}


推荐答案

您可以尝试使用行的 Files.lines(路径路径,Charset charset)表格来使用正确的编码方法( javadocs )。

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

这是支持编码的列表(无论如何,对于Oracle JVM)。 这篇文章表明Cp1252是Windows ANSI。

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

这篇关于java.nio.charset.MalformedInputException:输入长度= 1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆