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

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

问题描述

我有这个(去除了代码示例的 HTML 标签)函数,它可以从 CSV 中构建一个 HTML 表,但是每次我尝试运行它时都会出现运行时错误,我不知道为什么.Google 说可能是编码有问题,但我不知道如何更改.

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(Path path, Charset charset) 形式的 形式的正确编码>lines 方法(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天全站免登陆