How to mask a password from console input? I'm using Java 6.
I've tried using console.readPassword()
, but it wouldn't work. A full example might help me actually.
Here's my code:
import java.io.BufferedReader;
import java.io.Console;
import java.io.IOException;
import java.io.InputStreamReader;
public class Test
{
public static void main(String[] args)
{
Console console = System.console();
console.printf("Please enter your username: ");
String username = console.readLine();
console.printf(username + "
");
console.printf("Please enter your password: ");
char[] passwordChars = console.readPassword();
String passwordString = new String(passwordChars);
console.printf(passwordString + "
");
}
}
I'm getting a NullPointerException...
Question&Answers:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…