Java Tutorial/Development/Console — различия между версиями

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

Текущая версия на 15:30, 31 мая 2010

Console based login

import java.io.Console;
public class Login {
  public static void main(String[] args) throws Exception {
    Console console = System.console();
    String username = console.readLine("Username:");
    char[] pwd = console.readPassword("Password:");
    System.out.println("Username = " + username);
    System.out.println("Password = " + new String(pwd));
    username = "";
    for (int i = 0; i < pwd.length; i++)
      pwd[i] = 0;
  }
}





Console.ReadLine

import java.io.Console;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.text.MessageFormat;
import java.util.Date;
import java.util.Locale;
public class DateServer {
  public final static int PORT = 5000;
  private ServerSocket ss;
  public DateServer(int port) {
    try {
      ss = new ServerSocket(port);
    } catch (IOException ioe) {
      System.err.println("Unable to create server socket: " + ioe);
      System.exit(1);
    }
  }
  private void runServer() {
    Console console = System.console();
    Handler h = new Handler(ss);
    h.start();
    while (true) {
      String cmd = console.readLine(">");
      if (cmd == null)
        continue;
      if (cmd.equals("exit"))
        System.exit(0);
    }
  }
  public static void main(String[] args) {
    new DateServer(PORT).runServer();
  }
}
class Handler extends Thread {
  private ServerSocket ss;
  Handler(ServerSocket ss) {
    this.ss = ss;
  }
  public void run() {
    while (true) {
      try {
        Socket s = ss.accept();
        ObjectInputStream ois;
        ois = new ObjectInputStream(s.getInputStream());
        Locale l = (Locale) ois.readObject();
        PrintWriter pw;
        pw = new PrintWriter(s.getOutputStream());
        MessageFormat mf;
        mf = new MessageFormat("The date is {0, date, long}", l);
        Object[] args = { new Date() };
        pw.println(mf.format(args));
        pw.close();
      } catch (Exception e) {
        System.err.println(e);
      }
    }
  }
}





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





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