Where to get “UTF-8” string literal in Java?

前端 未结 11 858
谎友^
谎友^ 2020-12-02 03:51

I\'m trying to use a constant instead of a string literal in this piece of code:

new InputStreamReader(new FileInputStream(file), \"UTF-8\")
<
11条回答
  •  感情败类
    2020-12-02 04:47

    In Java 1.7+

    Do not use "UTF-8" string, instead use Charset type parameter:

    import java.nio.charset.StandardCharsets
    
    ...
    
    new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8);
    

提交回复
热议问题