在网上找了一些方法,大多使用的是启动线程用其他的字符覆盖密码,但是这样的做法没有达到预期的效果
username >duan
password >****出现了如下的效果username>duan
password>*******************************************s****s***f****h***********************************************************d*******d************
下面的是源代码
package NorvelScokect;import java.io.*;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;
   }
}package NorvelScokect;import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.io.InputStream;
import java.io.*;
import java.util.*;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;
   }
}package NorvelScokect;import java.io.IOException;class TestApp {
  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));
     }
  }}
请大家帮忙解决这个问题 谢谢

解决方案 »

  1.   

    /**
    *我这个方法不会显示 '*'号,输入的时候它什么不显示
    */
    import java.io.*;
    class ReadPass
    {
    public String readPass()throws Exception
    {
    InputStream in=System.in;
    int c;
    String str="";
    passInput pass=new passInput(in);
    pass.start();
    System.out.println("请输入密码:");
    while((c=in.read())!=-1)
    {
    if((char)c=='\r'||(char)c=='\n'){in.close();break;}
    str+=(char)c;
    }
    System.out.println("\n\n你输入的是:"+str);
    pass.stop();
    return str;
    }
    class passInput extends Thread
    {
    InputStream in;
    passInput(InputStream in){this.in=in;}
    public void run()
    {
    while(true)
    {
    if(this==Thread.currentThread())
    System.out.print("                  \r");
    }
    }
    }
    }
      

  2.   

    jdk1.6.0支持无回显的密码输入,很简单。