Java: Detect non-displayable chars for a given Character Encoding

前端 未结 2 1579
攒了一身酷
攒了一身酷 2021-01-14 14:32

I\'m currently working on an application to validate and parse CSV-files. The CSV files have to be encoded in UTF-8, although sometimes we get files in a false encoding. The

2条回答
  •  清酒与你
    2021-01-14 15:00

    Byte sequences that cannot be decoded correctly will be replaced with the "replacement character", \uFFFD, which is displayed like this: �. However, if the output device doesn't support that character, it is likely to use a question mark (?) instead.

    So, after decoding the UTF-8 data into String objects, search for occurrences of \uFFFD.

    Alternatively, if you set up an InputStreamReader with an instance of CharsetDecoder that you create yourself, you can get a lot more control. For example, you can specify that if any byte sequences that cannot be decoded, an Exception should be raised. Or you can ignore them. Or you can specify a different character as the replacement character.

提交回复
热议问题