从控制台窗口隐藏密码 Java 星号密码或阻止显示的输入字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27050323/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hide Password From Console Window Java Asterisk Password Or Block Inputted String Being Displayed
提问by Colin O'Croidheagain
public class Login {
protected static String user, pass, host;
Object conn;
public void Login() throws SQLException
{
try {
Class.forName ("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
System.out.println ("Could not load the driver");
}
Scanner sc = new Scanner(System.in);
Console console = System.console();
System.out.println("Please Enter Username");
user = sc.next();
System.out.println("Please Enter Password");
pass = sc.nextLine();
System.out.println("Please Enter Host/IP Address");
host = sc.next();
System.out.println("Attempting Log In");
Connection conn = (Connection) DriverManager.getConnection
("jdbc:mysql://"+host+":3306/chessdb", user, pass);
Just bit of help, been looking around online at blocking the password being shown on the console window. Is there a simple method of either the inputted text not appearing at all or else an asterisk to cover the input of the password field only?
只是有点帮助,一直在网上寻找阻止控制台窗口上显示的密码。是否有一种简单的方法,要么输入的文本根本不出现,要么只用星号覆盖密码字段的输入?
采纳答案by vembutech
Use the readPassword()
method.
使用readPassword()
方法。
instead of using scannerlike
而不是像使用扫描仪
System.out.println("Please Enter Password");
pass = sc.nextLine();
use:
利用:
System.out.println("Please Enter Password");
char[] passString = Console.readPassword();
String pass = new String(passString );
回答by Jaffar Ramay
Use java.io.Console.readPassword()
method
使用java.io.Console.readPassword()
方法
See API in below link
请参阅以下链接中的 API
https://docs.oracle.com/javase/7/docs/api/java/io/Console.html
https://docs.oracle.com/javase/7/docs/api/java/io/Console.html
回答by August
java.io.Console
has a method for reading passwords with output disabled: Console#readPassword
.
java.io.Console
有一种在禁用输出的情况下读取密码的方法:Console#readPassword
。