The java.lang.IO class provides simple methods for interactive console I/O. For standard console input, IO.readln()
should be preferred over the traditional, more verbose BufferedReader.
Traditionally, reading a line from the console required significant boilerplate code, involving InputStreamReader,
BufferedReader, and explicit IOException handling. This pattern is not only verbose but also error-prone and harder to
read.
The modern IO.readln() method:
try-with-resources) required for manual stream wrappers.Replace manual stream wrapping of System.in with the static IO.readln() method. If you are using a version of Java where
IO is automatically imported (such as in JShell or modern entry points), you can call IO.readln() directly; otherwise, use
import static java.lang.IO.readln;.
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
void main() {
try {
System.out.print("Enter text: ");
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String line = reader.readLine(); // Noncompliant: verbose boilerplate
System.out.println(line);
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
import static java.lang.IO.readln;
void main() {
// Compliant: concise and readable console input
String line = readln("Enter text: ");
System.out.println(line);
}