How to skip invalid characters in stream in Java/Scala?

前端 未结 4 1735
面向向阳花
面向向阳花 2021-02-02 12:53

For example I have following code

Source.fromFile(new File( path), \"UTF-8\").getLines()

and it throws exception

Exception in         


        
4条回答
  •  忘掉有多难
    2021-02-02 13:23

    You can influence the way that the charset decoding handles invalid input by calling CharsetDecoder.onMalformedInput.

    Usually you won't ever see a CharsetDecoder object directly, because it will be created behind the scenes for you. So if you need access to it, you'll need to use API that allows you to specify the CharsetDecoder directly (instead of just the encoding name or the Charset).

    The most basic example of such API is the InputStreamReader:

    InputStream in = ...;
    CharsetDecoder decoder = StandardCharsets.UTF_8.newDecoder();
    decoder.onMalformedInput(CodingErrorAction.IGNORE);
    Reader reader = new InputStreamReader(in, decoder);
    

    Note that this code uses the Java 7 class StandardCharsets, for earlier versions you can simply replace it with Charset.forName("UTF-8") (or use the Charsets class from Guava).

提交回复
热议问题