Files.readAllBytes vs Files.lines getting MalformedInputException

后端 未结 4 513
[愿得一人]
[愿得一人] 2020-12-05 19:05

I would have thought that the following two approaches to read a file should behave equally. But they don\'t. The second approach is throwing a MalformedInputException

4条回答
  •  广开言路
    2020-12-05 19:27

    To complement Jesper's answer, what happens here (and is undocumented!) is that Files.lines() creates a CharsetDecoder whose policy is to reject invalid byte sequences; that is, its CodingErrorAction is set to REPORT.

    This is unlike what happens for nearly all other Reader implementations provided by the JDK, whose standard policy is to REPLACE. This policy will result in all unmappable byte sequences to emit a replacement character (U+FFFD).

提交回复
热议问题