【问题标题】:Mask console user input [duplicate]屏蔽控制台用户输入[重复]
【发布时间】:2016-02-15 01:26:51
【问题描述】:

我正在制作一个基于控制台的应用程序,它会要求用户输入用户名和密码,将它们作为字符串存储到对象中,并将对象存储到基于引用的链表中。

有什么方法可以让用户输入密码时显示为“*****”而不是“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

【问题讨论】:

  • 标准java.io.Console只能隐藏密码,但不能用星号屏蔽输入符号。从头开始用星号实现屏蔽可能很乏味(而且不便携)。所以看看 JLine (jline.sourceforge.net) 库。这个 sn-p 将帮助String password = new jline.ConsoleReader().readLine(new Character('*'));

标签: java


【解决方案1】:

你可以使用控制台类

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 方法提供格式化提示,然后从禁用回显的控制台读取密码或密码。

【讨论】:

    猜你喜欢
    • 2011-12-29
    • 2011-04-09
    • 1970-01-01
    • 1970-01-01
    • 2013-02-13
    • 2011-03-25
    • 2015-04-27
    • 1970-01-01
    相关资源
    最近更新 更多