如何避免Files.lines()在日文文本上抛出MalformedInputException?

时间:2016-03-17 09:56:44

标签: java utf-8 character-encoding

我的代码抛出异常

Exception in thread "main" java.io.UncheckedIOException: java.nio.charset.MalformedInputException: Input length = 1
at java.io.BufferedReader$1.hasNext(BufferedReader.java:574)
at java.util.Iterator.forEachRemaining(Iterator.java:115)
at java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1801)
at java.util.stream.ReferencePipeline$Head.forEachOrdered(ReferencePipeline.java:590)
执行此行时

Files.lines(in.toPath()).forEachOrdered(new Consumer<String>() {
    @Override
    public void accept(String s) {
        //stuff
    });

在notepad ++中打开时导致此行为的文件,它显示如下 enter image description here

当然,由于编码,文本会出现乱码,如果我选择&#34;转换为UTF-8&#34;来自&#34;编码&#34;菜单然后它显示正确

enter image description here

这让我认为UTF-8是正确的编码,但是根据Java文档,Files.lines()默认使用UTF-8编码(它相当于Files.lines(path,StandardCharsets.UTF_8) ))但是当阅读带有日文文本的文件时它仍然会中断,我该如何处理?

0 个答案:

没有答案