How To Accept Password From Console in Java如何接受密码,从控制台在Java
Finally you can easily accept password from console in Java, without echoing them.最后,您可以很容易地接受密码,从控制台在Java中,没有附和他们。
This long unmet need has been addressed in这一长期未得到满足的需求已得到解决在 JDK 1.6的JDK 1.6 codeline. codeline 。
A new class -新一类- java.io.Console java.io.console has been defined to handle console I/O.已确定了处理控制台的I / O 。
To accept password from console in接受密码,从控制台在 JDK 6.0 (aka JDK 1.6)的JDK 6.0 (又名的JDK 1.6 ) use either:使用:
public char[] readPassword() 市民焦[ ] readpassword ( )
or或
public char[] readPassword(String fmt, Object… args) 市民焦[ ] readpassword (字符串裂变材料条约,对象… args )
Sample code: Console cons; char[] passwd; if ((cons = System.console()) != null && (passwd = cons.readPassword(”[%s]“, “Password:”)) != null) { // Use the passwd for authentication etc. ….; // Your code // After you are done zero the character array in memory for reducing security risk java.util.Arrays.fill(passwd, ‘ ‘); }示例代码:控制台的意见;烧焦[ ]密码;如果( (考虑= system.console ( ) ) ! =空& & (密码= cons.readpassword ( “ [ % ] ” , “密码: ” ) ) ! =空) ( / /使用密码进行身份验证等… … 。 ; / /您的代码/ /后,你是做了零的性质数组在内存中为减少安全风险java.util.arrays.fill (密码, ' ' ) ; ) So could I entice you yet to所以我想引诱你仍未 download JDK 1.6下载的JDK 1.6 ? ?
Filed under提起下 Headline News头条新闻 , , How To如何 , , Java Software Java软件 , , Tech Note技术说明 | |
| |
RSS 2.0 2.0 | |
Email this Article电子邮件此文章
You may also like to read您也可以想读 |




January 12th, 2006 at 10:45 am 2006年1月12日上午10时45分
[...] India Fourth Largest Internet User How To Accept Password From Console in Java » JDK1.6 / JDK 6.0 Downloaded? [ … … ]印度第四大互联网用户如何接受密码,从控制台在Java » jdk1.6 /的JDK 6.0下载? January 12th, 2006 by AngsumanChakraborty [...] 2006年1月12日由angsumanchakraborty [ … … ]
June 8th, 2006 at 6:21 am 2006年6月8日在上午06时21分
thanks for the service you provided to me.感谢您的服务提供给我。
November 19th, 2007 at 7:46 pm 2007年11月19日在下午7点46分
Console c = System.console();控制台c = system.console ( ) ;
if(c != null){如果(三! =空) (
String user = new String(c.readLine(”Enter user:”));字符串用户=新字串( c.readline ( “输入用户:"));
String pwd = new String(c.readPassword(”Enter pwd:”));字符串密码=新字串( c.readpassword ( “输入密码:"));
c.printf(”%20s”,user); c.printf ( “ % 20 ” ,用户) ;
c.printf(”\n%20s\n”,pwd); c.printf ( “ \频數百分比20多岁的\ n ” ,密码) ;
}else )否则
System.out.println(”Console is unavailable”); system.out.println ( “控制台是不可用” ) ;
注意運行環境為:jdk1.6+jcreator3.5.注意运行环境为: jdk1.6 + jcreator3.5 。
如果jcreator4.0的話,得到的c總是null.如果jcreator4.0的话,得到的c总是空。
March 28th, 2008 at 6:46 am 2008年3月28日在上午06时46分
it,s very nice它,硫很好