Tag: utf 8输入

如何检测非法的UTF-8字节序列以在java输入流中替换它们?

有问题的文件不在我的控制之下。 大多数字节序列都是有效的UTF-8,它不是ISO-8859-1(或其他编码)。 我想尽我所能提取尽可能多的信息。 该文件包含一些非法字节序列,应替换为替换字符。 这不是一件容易的事,它认为它需要一些关于UTF-8状态机的知识。 Oracle有一个包装器可以满足我的需求: UTF8ValidationFilter javadoc 是否有类似的东西(商业或免费软件)? 谢谢 -stephan 解: final BufferedInputStream in = new BufferedInputStream(istream); final CharsetDecoder charsetDecoder = StandardCharsets.UTF_8.newDecoder(); charsetDecoder.onMalformedInput(CodingErrorAction.REPLACE); charsetDecoder.onUnmappableCharacter(CodingErrorAction.REPLACE); final Reader inputReader = new InputStreamReader(in, charsetDecoder);