String readLine()
描述 (Description)
java.io.Console.reader()方法从控制台读取单行文本。
声明 (Declaration)
以下是java.io.Console.readLine()方法的声明 -
public String readLine()
参数 (Parameters)
NA
返回值 (Return Value)
此方法返回包含从控制台读取的行的字符串,不包括任何行终止字符,如果已到达流的末尾,则返回null。
异常 (Exception)
IOError - 如果发生I/O错误。
例子 (Example)
以下示例显示了java.io.Console.readLine()方法的用法。
package com.iowiki;
import java.io.Console;
public class ConsoleDemo {
public static void main(String[] args) {
Console cnsl = null;
String name = null;
try {
// creates a console object
cnsl = System.console();
// if console is not null
if (cnsl != null) {
// read line from the user input
name = cnsl.readLine("Name: ");
// prints
System.out.println("Name entered : " + name);
}
} catch(Exception ex) {
// if any error occurs
ex.printStackTrace();
}
}
}
让我们编译并运行上面的程序,这将产生以下结果 -
Name: Master Programmer
Name entered : Master Programmer