在java6之后可以使用console.readPassword()方法隐藏用户输入的文本。
如下示例代码:
```java import java.io.Console;
/ * @author outofmemory.cn / public class Main {
/**
* 在控制台输入密码示例
*/
public void passwordExample() {
Console console = System.console();
if (console == null) {
System.out.println("Couldn't get Console instance, maybe you're running this from within an IDE?");
System.exit(0);
}
console.printf("Testing password%n");
char passwordArray[] = console.readPassword("Enter your secret password: ");
console.printf("Password entered was: %s%n", new String(passwordArray));
}
/**
* @param args 命令行参数
*/
public static void main(String[] args) {
new Main().passwordExample();
}
如下示例代码:
```java import java.io.Console;
/ * @author outofmemory.cn / public class Main {
/**
* 在控制台输入密码示例
*/
public void passwordExample() {
Console console = System.console();
if (console == null) {
System.out.println("Couldn't get Console instance, maybe you're running this from within an IDE?");
System.exit(0);
}
console.printf("Testing password%n");
char passwordArray[] = console.readPassword("Enter your secret password: ");
console.printf("Password entered was: %s%n", new String(passwordArray));
}
/**
* @param args 命令行参数
*/
public static void main(String[] args) {
new Main().passwordExample();
}