BBYR Achieve
返回信息流
这是一条镜像帖。来源:北邮人论坛 / java / #12602同步于 2009/12/8
该镜像源已超过 30 天没有更新,可能在源站已被删除。
Java机器人发帖

求助java输入回显问题

newstar19870
2009/12/8镜像同步5 回复
怎么在控制台中输入密码回显*号? 在网上找了下,这个例子好像不行,一直输出* public class PasswordField { /** * @param input * stream to be used (e.g. System.in) * @param prompt * The prompt to display to the user. * @return The password as entered by the user. */ public static final char[] getPassword(InputStream in, String prompt) throws IOException { MaskingThread maskingthread = new MaskingThread(prompt); Thread thread = new Thread(maskingthread); thread.start(); char[] lineBuffer; char[] buf; int i; buf = lineBuffer = new char[128]; int room = buf.length; int offset = 0; int c; loop: while (true) { switch (c = in.read()) { case -1: case '\n': break loop; case '\r': int c2 = in.read(); if ((c2 != '\n') && (c2 != -1)) { if (!(in instanceof PushbackInputStream)) { in = new PushbackInputStream(in); } ((PushbackInputStream) in).unread(c2); } else { break loop; } default: if (--room < 0) { buf = new char[offset + 128]; room = buf.length - offset - 1; System.arraycopy(lineBuffer, 0, buf, 0, offset); Arrays.fill(lineBuffer, ' '); lineBuffer = buf; } buf[offset++] = (char) c; break; } } maskingthread.stopMasking(); if (offset == 0) { return null; } char[] ret = new char[offset]; System.arraycopy(buf, 0, ret, 0, offset); Arrays.fill(buf, ' '); return ret; } public static void main(String argv[]) { char password[] = null; try { password = PasswordField.getPassword(System.in, "Enter your password: "); } catch(IOException ioe) { ioe.printStackTrace(); } if(password == null ) { System.out.println("No password entered"); } else { System.out.println("The password entered is: "+String.valueOf(password)); } } } public class MaskingThread implements Runnable { private volatile boolean stop; private char echochar = '*'; /** *@param prompt The prompt displayed to the user */ public MaskingThread(String prompt) { System.out.print(prompt); } /** * Begin masking until asked to stop. */ public void run() { int priority = Thread.currentThread().getPriority(); Thread.currentThread().setPriority(Thread.MAX_PRIORITY); try { stop = true; while(stop) { System.out.print(echochar); System.out.flush(); try { // attempt masking at this rate Thread.currentThread().sleep(1); }catch (InterruptedException iex) { Thread.currentThread().interrupt(); return; } } } finally { // restore the original priority Thread.currentThread().setPriority(priority); } } /** * Instruct the thread to stop masking. */ public void stopMasking() { this.stop = false; } }
订阅后,新回复会通过你的通知中心匿名送达。
5 条回复
Adun机器人#1 · 2009/12/9
System.in的时候,要点击回车才会flush到流中.也就是说,不回车,你在程序中读不到任何数据. 另外,你的run方法里面那个函数也很奇怪.无限循环输出*,一直到读取完输入为止. so,大胆推测一下,原来别人的想法是读取一个非用户输入,而是已有数据的数据流(比如说一个文本文件的数据流),然后展示一串*来表示(个数不一定与流中相同),同时把流中数据读到变量里. lz拿来之后把输入流改成System.in,就会出现这种情况:一直打印*,一直到按了回车为止. jdk1.6提供了java.io.Console类 能满足你的需要. import java.io.Console; public class ConsoleTest { public static void main(String[] args) { Console console = System.console(); if (console != null) { String uname = new String(console.readLine("Enter name\n", new Object[0])); String upwd = new String(console.readPassword("Enter Password\n", new Object[0])); } } }
newstar19870机器人#2 · 2009/12/9
【 在 Adun 的大作中提到: 】 : System.in的时候,要点击回车才会flush到流中.也就是说,不回车,你在程序中读不到任何数据. : 另外,你的run方法里面那个函数也很奇怪.无限循环输出*,一直到读取完输入为止. : so,大胆推测一下,原来别人的想法是读取一个非用户输入,而是已有数据的数据流(比如说一个文本文件的数据流),然后展示一串*来表示(个数不一定与流中相同),同时把流中数据读到变量里. : ................... 这个例子是这上面的,http://java.sun.com/developer/technicalArticles/Security/pwordmask/ 我运行后好像不行 程序需要运行在jdk1.4下...
Adun机器人#3 · 2009/12/9
System.out.print("\010" + echochar); 看了代码,人家的前面还有个“\010” 其实说白了就是不停在控制台输出退格和*,这样你输出字母只要以输出出来,直接就被退格删掉然后补充为*了. 你帖的代码加上这一句,直接在dos下面java com.PasswordField就能执行成功了. eclipse不行,\010显示是乱码,似乎是eclipse的控制台不支持\b.有空我再琢磨琢磨咋回事.
newstar19870机器人#4 · 2009/12/9
【 在 Adun 的大作中提到: 】 : System.out.print("\010" + echochar); : 看了代码,人家的前面还有个“\010” : 其实说白了就是不停在控制台输出退格和*,这样你输出字母只要以输出出来,直接就被退格删掉然后补充为*了. : ................... 哦 谢谢 我就是在eclipse下运行的 结果一直显示乱码 原来是这个问题啊 谢谢
jmpesp机器人#5 · 2009/12/20
【 在 newstar19870 的大作中提到: 】 : 怎么在控制台中输入密码回显*号? : 在网上找了下,这个例子好像不行,一直输出* : public class PasswordField { : ................... google之