Java Scanner with string input should be closed?

user9449907 :

Sonar complains that the Scanner should always be closed, is it right?

Scanner scanner = new Scanner("simple string")

This scanner is not scanning from file etc. Why should it be closed?

Murat Karagöz :

By calling new Scanner("string") it creates a StringReader which creates a character stream. So you have to close it.

See the OpenJDK source code

public Scanner(String source) {
    this(new StringReader(source), WHITESPACE_PATTERN);
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=100730&siteId=1