हाँ किया जा सकता है। इसे कमांड लाइन इनपुट मास्किंग कहा जाता है। आप इसे आसानी से कार्यान्वित कर सकते हैं।
आप प्रतिबिंबित वर्णों को मिटाने के लिए अलग थ्रेड का उपयोग कर सकते हैं, और उन्हें तारों से बदल दिया जाता है। इस विवरण में चर्चा इस सूत्र
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;
}
}
This Link का उपयोग कर के साथ 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;
}
}
दिखाया उपयोग किया जाता है।
स्रोत
2012-05-30 15:35:52
+1 मुझे यह बताने के लिए कि यह ग्रहण कंसोल में काम नहीं करता है (हालांकि मैं नेटबीन्स का उपयोग करता हूं, जाहिर है कि यह किसी भी में काम नहीं करता है)। – kentcdodds
नमूना अभ्यास: http://www.tutorialspoint.com/java/io/console_readpassword.htm – mavis
अधिकांश आईडीई में काम नहीं करते क्योंकि वे जावा का उपयोग करते हैं। Http://stackoverflow.com/a/26473083/3696510 – muttonUp