SonarQube 说我 "remove this use of constructor "Scanner(InputStream)”
SonarQube saying me "remove this use of constructor "Scanner(InputStream)"
我应该使用什么来代替 Scanner,这样 SonarQube 就不会报错?
final Scanner scan = new Scanner(System.in);
这是因为SonarQube中的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.
要修复它,您可以使用具有已定义编码类型的扫描器:
/**
* Constructs a new <code>Scanner</code> that produces values scanned
* from the specified input stream. Bytes from the stream are converted
* into characters using the specified charset.
*
* @param source An input stream to be scanned
* @param charsetName The encoding type used to convert bytes from the
* stream into characters to be scanned
* @throws IllegalArgumentException if the specified character set
* does not exist
*/
public Scanner(InputStream source, String charsetName) {
this(makeReadable(Objects.requireNonNull(source, "source"), toCharset(charsetName)),
WHITESPACE_PATTERN);
}
我应该使用什么来代替 Scanner,这样 SonarQube 就不会报错?
final Scanner scan = new Scanner(System.in);
这是因为SonarQube中的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.
要修复它,您可以使用具有已定义编码类型的扫描器:
/**
* Constructs a new <code>Scanner</code> that produces values scanned
* from the specified input stream. Bytes from the stream are converted
* into characters using the specified charset.
*
* @param source An input stream to be scanned
* @param charsetName The encoding type used to convert bytes from the
* stream into characters to be scanned
* @throws IllegalArgumentException if the specified character set
* does not exist
*/
public Scanner(InputStream source, String charsetName) {
this(makeReadable(Objects.requireNonNull(source, "source"), toCharset(charsetName)),
WHITESPACE_PATTERN);
}