Reliance on default encoding, what should I use and why?

前端 未结 4 1788
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-28 14:36

FindBugs reports a bug:

Reliance on default encoding Found a call to a method which will perform a byte to String (or String to byte) conversion, a

4条回答
  •  盖世英雄少女心
    2020-12-28 14:58

    Ideally, it should be:

    try (InputStream in = new FileInputStream(file);
         Reader reader = new InputStreamReader(in, StandardCharsets.UTF_8);
         BufferedReader br = new BufferedReader(reader)) {
    

    ...or:

    try (BufferedReader br = Files.newBufferedReader(path, StandardCharsets.UTF_8)) {
    

    ...assuming the file is encoded as UTF-8.

    Pretty much every encoding that isn't a Unicode Transformation Format is obsolete for natural language data. There are languages you cannot support without Unicode.

提交回复
热议问题