Java/JDK 6/Console

Материал из Java эксперт
Перейти к: навигация, поиск

Console output with format

  

import java.io.Console;
import java.sql.SQLException;
public class MainClass {
  public static void main(String[] args) throws ClassNotFoundException, SQLException {
    Console console = System.console();
    if (console == null) {
      System.err.println("sales: unable to obtain console");
      return;
    }
    console.printf("%s ", "string");
  }
}





Console read Password

  
import java.io.Console;
import java.sql.SQLException;
public class MainClass {
  public static void main(String[] args) throws ClassNotFoundException, SQLException {
    Console console = System.console();
    if (console == null) {
      System.err.println("sales: unable to obtain console");
      return;
    }
    String password = new String (console.readPassword ("Enter password: "));
    System.out.println(password);
  }
}





Java Console readLine

  

import java.io.Console;
import java.sql.SQLException;
public class MainClass {
  public static void main(String[] args) throws ClassNotFoundException, SQLException {
    Console console = System.console();
    if (console == null) {
      System.err.println("sales: unable to obtain console");
      return;
    }
    String username = console.readLine("Enter username: ");
    System.out.println(username);
  }
}





Masking a password with the Console class

 
import java.io.Console;
public class Main {
  public static void main(String[] args) {
    Console console = System.console();
    char passwordArray[] = console.readPassword("Enter your secret password: ");
    console.printf("Password entered was: %s%n", new String(passwordArray));
  }
}





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);
    }
  }
}





Printing text with the Console class

 
import java.io.Console;
public class Main {
  public static void main(String[] args) {
    Console console = System.console();
    console.printf("%s%n", "this is a test");
  }
}





Use Console class to read user input?

 

import java.io.Console;
import java.util.Arrays;
public class Main {
  public static void main(String[] args) {
    Console console = System.console();
    String username = console.readLine("Username: ");
    char[] password = console.readPassword("Password: ");
    if (username.equals("admin") && String.valueOf(password).equals("secret")) {
      console.printf("Welcome to Java Application %1$s.\n", username);
      Arrays.fill(password, " ");
    } else {
      console.printf("Invalid username or password.\n");
    }
  }
}