char[] readPassword(String fmt, Object... args)
描述 (Description)
java.io.Console.readPassword(String fmt, Object... args)方法提供格式化提示,然后从控制台读取密码并禁用回显。
声明 (Declaration)
以下是java.io.Console.readPassword(String fmt, Object... args)方法的声明 -
public char[] readPassword(String fmt, Object... args)
参数 (Parameters)
fmt - 提示文本的格式字符串语法中描述的格式字符串。
args - 格式字符串中格式说明符引用的参数。
返回值 (Return Value)
此方法返回一个字符数组,其中包含从控制台读取的密码,不包括行终止字符;如果已到达流的末尾,则返回null。
异常 (Exception)
IllegalFormatException - 如果格式字符串包含非法语法,格式说明符与给定参数不兼容,给定格式字符串的参数不足或其他非法条件。
IOError - 如果发生I/O错误。
例子 (Example)
以下示例显示了java.io.Console.readPassword(String fmt,Object ... args)方法的用法。
package com.iowiki;
import java.io.Console;
public class ConsoleDemo {
public static void main(String[] args) {
Console cnsl = null;
String alpha = null;
String fmt = "%2$5s %3$10s%n";
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,"Your","Name: ");
// prints
System.out.println("Name is: " + alpha);
// read password into the char array
char[] pwd = cnsl.readPassword(fmt,"Enter","Password: ");
// prints
System.out.println("Password is: "+pwd);
}
} catch(Exception ex) {
// if any error occurs
ex.printStackTrace();
}
}
}
让我们编译并运行上面的程序,这将产生以下结果 -
Your Name: Master Programmer
Name is: Master Programmer
Enter Password:
Password is: Good Morning