This question already has answers here:
Masking password input from the console : Java
                                
                                    (5个答案)
                                
                        
                                4年前关闭。
            
                    
我正在制作一个基于控制台的应用程序,它将要求用户输入用户名和密码,将它们作为字符串存储到一个对象中,然后将该对象存储到基于引用的链接列表中。

有什么方法可以使用户输入密码时,系统(显然)显示为“ 12345”时,密码显示为“ *****”而不是“ 12345”?

我有一个带有适合用户输入的方法的类。

System.out.println("Username: ");
String un = EasyIn.getString();
System.out.println("Password: ");
String pw = reader.getString();
System.out.println("Repeat Password: ");
String pwRepeat = reader.getString();
//and then go on to confirm that the passwords match and store them in the object etc

最佳答案

您可以使用控制台类

http://docs.oracle.com/javase/6/docs/api/java/io/Console.html

和读取密码方法:
http://docs.oracle.com/javase/6/docs/api/java/io/Console.html#readPassword()

readPassword方法提供格式化的提示,然后从控制台读取禁用回显的密码或密码。

08-05 07:39
查看更多