import *; import java.util.*; public class 无回显密码输入 { public static void main(String[] args) { Console console = System.console(); if (console == null) { System.err.println("Failed to get console!"); return; } String name = console.readLine("Please type your name:"); char[] password1 = null; char[] password2 = null; console.printf("Your name is %s\n", name);
password1 = console.readPassword("Please type your password:"); password2 = console.readPassword("Please type your password again:"); if (!Arrays.equals(password1, password2)) { System.err.println("Password doesn't match the confirmation."); return; } console.printf("Hi, %s, Your password is '%s'\n", name, new String(password1)); Arrays.fill(password1, '\0'); /* 安全清理 */ Arrays.fill(password2, '\0'); } }