String readLine(String fmt, Object... args)
描述 (Description)
java.io.Console.readLine(String fmt, Object... args)方法提供格式化提示,然后从控制台读取单行文本。
声明 (Declaration)
以下是java.io.Console.readLine(String fmt, Object... args)方法的声明 -
public String readLine(String fmt, Object... args)
参数 (Parameters)
fmt
args
返回值 (Return Value)
此方法返回包含从控制台读取的行的字符串,不包括任何行终止字符;如果已到达流的末尾,则返回null。
异常 (Exception)
IllegalFormatException - 如果格式字符串包含非法语法,格式说明符与给定参数不兼容,给定格式字符串的参数不足或其他非法条件。
IOError - 如果发生I/O错误。
例子 (Example)
以下示例显示了java.io.Console.readLine(String fmt,Object ... args)方法的用法。
package com.iowiki;
import java.io.Console;
public class ConsoleDemo {
public static void main(String[] args) {
Console cnsl = null;
String fmt = "%1$4s %2$5s %3$10s%n";
String alpha = null;
try {
// creates a console object
cnsl = System.console();
// if console is not null
if (cnsl != null) {
// read line from the user input
alpha = cnsl.readLine(fmt, "Enter","Alphabets: ");
// prints
System.out.println("Alphabets entered: " + alpha);
}
} catch(Exception ex) {
// if any error occurs
ex.printStackTrace();
}
}
}
让我们编译并运行上面的程序,这将产生以下结果 -
Enter Alphabets: ABCDEFGHIJKLMNOPQRSTUVWXYZ
Alphabets entered : ABCDEFGHIJKLMNOPQRSTUVWXYZ