java.nio.charset.malformedInputException: Input length = 1
2022-09-01 18:51:05
我有这个(剥离了代码示例的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();
}
}