我有这个(去除代码示例的 HTML 标记)函数,它从 CSV 构建 HTML 表,但每次尝试运行它时都会出现运行时错误,我不知道为什么。谷歌说可能编码有问题,但我不知道如何改变它。
我的 CSV 以 ANSI 编码并包含 ä、Ä、Ü、Ö 等字符,但我无法控制编码或将来是否会更改。
错误发生在这里:
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();
}
}