<分区>
我正在尝试使用流来读取文件,但我无法通过异常。我一直在环顾四周,但我就是不明白为什么会抛出它。
我要读取的文件是 file.txt
,它是用 UTF-8 编码的。
我正在使用 Files.lines()
阅读它:
String path = FileWordCount.class.getResource("file.txt").getPath().substring(1);
Files.lines(Paths.get(path), Charset.forName("UTF-8")).forEach(System.out::println);
尝试读取文件时出现以下异常:
Exception in thread "main" java.io.UncheckedIOException: java.nio.charset.MalformedInputException: Input length = 1
[...]
Caused by: java.nio.charset.MalformedInputException: Input length = 1
通常我不会发布关于异常的简单问题,但我只是弄清楚这个问题。