删除构造函数 "Scanner(file)" 的这种使用

Remove this use of constructor "Scanner(file)"

如何解决这个声纳问题?

File file = new File("/some directory");
Scanner scanner = new Scanner(file);

删除构造函数“Scanner(file)”的这种使用

rule 说:

Classes and methods that rely on the default system encoding should not be used

Using classes and methods that rely on the default system encoding can result in code that works fine in its "home" environment. But that code may break for customers who use different encodings in ways that are extremely difficult to diagnose and nearly, if not completely, impossible to reproduce when it's time to fix them.

要解决此问题,您应该使用包含 Charset 的构造函数。例如,Scanner(File, Charset) 构造函数。您应该指定所需的字符集。

File file = new File("/some directory");
Scanner scanner = new Scanner(file, StandardCharsets.UTF_8); // as an example