如下用线程实现在命令行下密码屏蔽的功能:MaskingThread 类:
package com.namitta.abn.ibhsm.pwin;import java.io.*;/**
* This class attempts to erase characters echoed to the console.
*/class MaskingThread extends Thread {
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("\010" + echochar);
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;
}
}PasswordApp 类
package com.namitta.abn.ibhsm.pwin;import java.io.*;
import java.util.*;/**
* This class prompts the user for a password and attempts to mask input with "*"
*/public class PasswordApp { /**
* Get the A and B code from System.in
*
* @return object
*/
public static Object startIn() {
char[][] psw = new char[2][];
char passwordA[] = null;
char passwordB[] = null;
try {
passwordA = PasswordApp.getPassword(System.in,"Enter The Code NumberA: ");
passwordB = PasswordApp.getPassword(System.in,"Enter The Code NumberB: ");
} catch (IOException ioe) {
ioe.printStackTrace();
}
if (passwordA == null || passwordA == null) {
System.out.println("No password entered");
return null;
}
psw[0] = passwordA;
psw[1] = passwordB;
return psw; } /**
*@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;
buf = lineBuffer = new char[48];
int room = buf.length;
int offset = 0;
int c; loop: while (true) {
switch (c = in.read()) {
case -1:
case '\n':
break loop;
default:
if (offset >=48 ) {
break loop; //在此处输满48个字符后,为什么不能break 出来???????
/* 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;
}
}新手求救中!!
package com.namitta.abn.ibhsm.pwin;import java.io.*;/**
* This class attempts to erase characters echoed to the console.
*/class MaskingThread extends Thread {
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("\010" + echochar);
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;
}
}PasswordApp 类
package com.namitta.abn.ibhsm.pwin;import java.io.*;
import java.util.*;/**
* This class prompts the user for a password and attempts to mask input with "*"
*/public class PasswordApp { /**
* Get the A and B code from System.in
*
* @return object
*/
public static Object startIn() {
char[][] psw = new char[2][];
char passwordA[] = null;
char passwordB[] = null;
try {
passwordA = PasswordApp.getPassword(System.in,"Enter The Code NumberA: ");
passwordB = PasswordApp.getPassword(System.in,"Enter The Code NumberB: ");
} catch (IOException ioe) {
ioe.printStackTrace();
}
if (passwordA == null || passwordA == null) {
System.out.println("No password entered");
return null;
}
psw[0] = passwordA;
psw[1] = passwordB;
return psw; } /**
*@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;
buf = lineBuffer = new char[48];
int room = buf.length;
int offset = 0;
int c; loop: while (true) {
switch (c = in.read()) {
case -1:
case '\n':
break loop;
default:
if (offset >=48 ) {
break loop; //在此处输满48个字符后,为什么不能break 出来???????
/* 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;
}
}新手求救中!!
楼主【feptest】截止到2008-06-30 18:24:21的历史汇总数据(不包括此帖):
发帖数:0 发帖分:0
结贴数:0 结贴分:0
未结数:0 未结分:0
结贴率:-------------------结分率:-------------------
如何结贴请参考这里:http://topic.csdn.net/u/20080501/09/ef7ba1b3-6466-49f6-9d92-36fe6d471dd1.html
MaskingThread maskingthread = new MaskingThread(prompt);
Thread thread = new Thread(maskingthread);
thread.start();
直接maskingthread .start()就可以了
String password = null;
Console console = System.console();
System.out.print("Please Input Password: ");
char[] pw = console.readPassword();
password = new String(pw);
System.out.println("Your Inputed Password Is: " + password);
}}