java无回显的密码输入

int32位 posted @ Sep 09, 2014 04:45:17 PM in java , 6390 阅读
转载请注明:http://krystism.is-programmer.com/若有错误,请多多指正,谢谢!

在java6中有一个新类Console,能够实现无回显输入,用于安全输入!以下是代码样例:

import java.io.*;
import java.util.*;
public class ConsoleDemo {
    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');
    }
}
转载请注明:http://krystism.is-programmer.com/若有错误,请多多指正,谢谢!
  • 无匹配
  • 无匹配

登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter