我正在尝试在控制台中运行 java 应用程序。这是我使用的用户名和密码的代码,console.readPassword()
当我在控制台中输入我的密码时,光标出现在同一条起始行上,并且在我输入完整密码并单击Enter按钮之前一直保持在同一位置。
Console console = System.console();
console.printf("Please enter your username: ");
String username = console.readLine();
console.printf(username + "\n");
console.printf("Please enter your password: ");
char[] passwordChars = console.readPassword();
console.printf("Password entered was: %s%n", new String("#####"));
String password = new String(passwordChars);
此代码在输入完整密码后屏蔽密码,但在用户输入密码时不显示任何光标移动。
我的想法是用特殊字符和正确的光标移动来掩盖密码,这不会让用户感到困惑。有人可以帮我弄这个吗?