转自:
http://article.yeeyan.org/bilingual/316748
代码如下:
Source.fromFile(new File( path), "UTF-8").getLines()
运行这段代码时抛出一个MalformedInputException, 异常内容如下:
Exception in thread "main" java.nio.charset.MalformedInputException: Input length = 1
at java.nio.charset.CoderResult.throwException(CoderResult.java:260)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:319)
我不关心是否文件中所有行都被读取,怎么才能让代码在读取文件内容时略过无效字符?
你可以通过调用这个函数来控制字节流解析行为:
CharsetDecoder.onMalformedInput
由于函数调用时会自动创建 CharsetDecoder 对象,因此,在通常情况下, 函数调用时一般不需要传递 CharsetDecoder 对象。当需要定制该对象行为时,必须显示定义 CharsetDecoder 对象, 而不是只使用字符集名称。
参考Java API中的 InputStreamReader 的处理过程:
InputStream in = ...;
CharsetDecoder decoder = StandardCharset.UTF_8.newDecoder();
decoder.onMalformedInput(CodingErrorAction.IGNORE);
Reader reader = new InputStreamReader(in, decoder);
需要注意下, 这段代码需要引用 Java 7 中的 StandardCharset , 对于早期版本, 可以使用标准类中的 Charset.forName("UTF-8") 或 Guava 中的 theCharsetsclass 。