gpt4 book ai didi

java - 隐藏命令行输入

转载 作者:IT老高 更新时间:2023-10-28 20:23:28 29 4
gpt4 key购买 nike

我知道 Git 和其他命令行界面能够隐藏用户的输入(对密码很有用)。有没有办法在Java中以编程方式做到这一点?我正在从用户那里获取密码输入,我希望他们的输入被“隐藏”在该特定行上(但不是在所有行上)。这是我的代码(虽然我怀疑它会有所帮助......)

try (Scanner input = new Scanner(System.in)) {
//I'm guessing it'd probably be some property you set on the scanner or System.in right here...
System.out.print("Please input the password for " + name + ": ");
password = input.nextLine();
}

最佳答案

试试 java.io.Console.readPassword .不过,您必须至少运行 Java 6。

   /**
* Reads a password or passphrase from the console with echoing disabled
*
* @throws IOError
* If an I/O error occurs.
*
* @return A character array containing the password or passphrase read
* from the console, not including any line-termination characters,
* or <tt>null</tt> if an end of stream has been reached.
*/
public char[] readPassword() {
return readPassword("");
}

但请注意,这个 doesn't work使用 Eclipse 控制台。您必须从 true 控制台/shell/终端/提示符运行程序才能对其进行测试。

关于java - 隐藏命令行输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10819469/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com