Java.io.Console.readPassword() 方法
描述
java.io.Console.readPassword() 方法从禁用回显的控制台读取密码。
声明
以下是 java.io.Console.readPassword() 方法的声明 −
public char[] readPassword()
参数
NA
返回值
此方法返回一个字符数组,其中包含从控制台读取的密码,不包括行终止字符,如果已到达流的结尾,则返回 null。
异常
IOError − 如果发生 I/O 错误。
示例
下面的例子展示了 java.io.Console.readPassword() 方法的使用。
package com.tutorialspoint;
import java.io.Console;
public class ConsoleDemo {
public static void main(String[] args) {
Console cnsl = null;
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("Name: ");
// prints
System.out.println("Name is: " + alpha);
// read password into the char array
char[] pwd = cnsl.readPassword("Password: ");
// prints
System.out.println("Password is: "+pwd);
}
} catch(Exception ex) {
// if any error occurs
ex.printStackTrace();
}
}
}
让我们编译并运行上面的程序,这将产生下面的结果 −
Name: Master Programmer Name is: Master Programmer Password: Password is: Good Morning

