下面看一下下面的代码:
import java.io.Console;
public class Main {
public static void main(String[] args){
Console console = System.console();
String password ;
password = new String(console.readPassword());
System.out.println("password="+password);
}
}程序运行后,输入字符没有显示,回车后,打印出结果.如下图
看到这里,大家可能都希望能够输入一个字符,显示一个星号之类的,这样有一定的提示作用.于是我查看了Console的源码,发现通过Console是实现不了的
public char[] readPassword() {
return readPassword("");
}
public char[] readPassword(String fmt, Object ... args) {
char[] passwd = null;
synchronized (writeLock) {
synchronized(readLock) {
try {
echoOff = echo(false);
} catch (IOException x) {
throw new IOError(x);
}
IOError ioe = null;
try {
if (fmt.length() != 0)
pw.format(fmt, args);
passwd = readline(true);
} catch (IOException x) {
ioe = new IOError(x);
} finally {
try {
echoOff = echo(true);
} catch (IOException x) {
if (ioe == null)
ioe = new IOError(x);
else
ioe.addSuppressed(x);
}
if (ioe != null)
throw ioe;
}
pw.println();
}
}
return passwd;
}
原文:http://blog.csdn.net/dliyuedong/article/details/21456325