gpt4 book ai didi

java - SonarQube 说我 "remove this use of constructor "扫描仪(InputStream)”

转载 作者:行者123 更新时间:2023-11-30 05:23:04 26 4
gpt4 key购买 nike

我应该使用什么来代替扫描仪,这样 SonarQube 就不会提示?

final Scanner scan = new Scanner(System.in);

最佳答案

这是因为以下rule在 SonarQube 中:

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);
}

关于java - SonarQube 说我 "remove this use of constructor "扫描仪(InputStream)”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59194826/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com