Password Prompting with java.io.Console : Console « JDK 6 « Java






Password Prompting with java.io.Console

  

import java.io.Console;
import java.util.Arrays;

public class PasswordPromptingDemo {

  public static void main(String[] args) {
    Console console = System.console();

    if (console == null) {
      System.out.println("Console is not available");
      System.exit(1);
    }

    char[] password = "mustang".toCharArray();

    char[] passwordEntered = console.readPassword("Enter password: ");

    if (Arrays.equals(password, passwordEntered)) {
      System.out.println("\n Access granted \n");
      Arrays.fill(password, ' ');
      Arrays.fill(passwordEntered, ' ');
      System.out.println("OK ...");
    } else {
      System.out.println("Access denied");
      System.exit(1);
    }
  }
}

          
  








Related examples in the same category

1.Java Console readLine
2.Console read Password
3.Console output with format
4.Printing text with the Console class
5.Masking a password with the Console class
6.Use Console class to read user input?