java命令行读入密码_java-在命令行上隐藏输入
是的,可以做。 這稱為命令行輸入屏蔽。 您可以輕松實現此目的。
您可以使用單獨的線程擦除輸入的回顯字符,并用星號替換。 使用下面顯示的EraserThread類完成此操作
import java.io.*;
class EraserThread implements Runnable {
private boolean stop;
/**
*@param The prompt displayed to the user
*/
public EraserThread(String prompt) {
System.out.print(prompt);
}
/**
* Begin masking...display asterisks (*)
*/
public void run () {
stop = true;
while (stop) {
System.out.print("\010*");
try {
Thread.currentThread().sleep(1);
} catch(InterruptedException ie) {
ie.printStackTrace();
}
}
}
/**
* Instruct the thread to stop masking
*/
public void stopMasking() {
this.stop = false;
}
}
使用此線程
public class PasswordField {
/**
*@param prompt The prompt to display to the user
*@return The password as entered by the user
*/
public static String readPassword (String prompt) {
EraserThread et = new EraserThread(prompt);
Thread mask = new Thread(et);
mask.start();
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String password = "";
try {
password = in.readLine();
} catch (IOException ioe) {
ioe.printStackTrace();
}
// stop masking
et.stopMasking();
// return the password entered by the user
return password;
}
}
此鏈接詳細討論。
總結
以上是生活随笔為你收集整理的java命令行读入密码_java-在命令行上隐藏输入的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Jquery DIV滚动至浏览器顶部后固
- 下一篇: Strutsw2与Spring整合流程-