java.io.Console类的使用及代码示例

x33g5p2x  于2022-01-18 转载在 其他  
字(7.1k)|赞(0)|评价(0)|浏览(203)

本文整理了Java中java.io.Console类的一些代码示例,展示了Console类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Console类的具体详情如下:
包路径:java.io.Console
类名称:Console

Console介绍

[英]Provides access to the console, if available. The system-wide instance can be accessed via java.lang.System#console.
[中]提供对控制台的访问(如果可用)。可以通过java访问系统范围的实例。lang.System#控制台。

代码示例

代码示例来源:origin: stackoverflow.com

private String readLine(String format, Object... args) throws IOException {
  if (System.console() != null) {
    return System.console().readLine(format, args);
  }
  System.out.print(String.format(format, args));
  BufferedReader reader = new BufferedReader(new InputStreamReader(
      System.in));
  return reader.readLine();
}

private char[] readPassword(String format, Object... args)
    throws IOException {
  if (System.console() != null)
    return System.console().readPassword(format, args);
  return this.readLine(format, args).toCharArray();
}

代码示例来源:origin: btraceio/btrace

@Override
public void handle(Signal sig) {
  try {
    con.printf("Please enter your option:\n");
    con.printf("\t1. exit\n\t2. send an event\n\t3. send a named event\n\t4. flush console output\n");
    con.flush();
    String option = con.readLine();
    option = option.trim();
    if (option == null) {
        break;
      case "3":
        con.printf("Please enter the event name: ");
        String name = con.readLine();
        if (name != null) {
          if (isDebug()) debugPrint("sending event command");
        out.flush();
        break;
      default:
        con.printf("invalid option!\n");
        break;

代码示例来源:origin: btraceio/btrace

@SuppressWarnings("DefaultCharset")
private static PrintWriter getOutWriter(Console con) {
  return (con != null)? con.writer() : new PrintWriter(System.out);
}

代码示例来源:origin: robovm/robovm

/**
 * Reads a password from the console. The password will not be echoed to the display.
 * A formatted prompt is also displayed.
 *
 * @param format the format string (see {@link java.util.Formatter#format})
 * @param args
 *            the list of arguments passed to the formatter. If there are
 *            more arguments than required by {@code format},
 *            additional arguments are ignored.
 * @return a character array containing the password, or null at EOF.
 */
public char[] readPassword(String format, Object... args) {
  synchronized (CONSOLE_LOCK) {
    format(format, args);
    return readPassword();
  }
}

代码示例来源:origin: robovm/robovm

/**
 * Reads a line from this console, using the specified prompt.
 * The prompt is given as a format string and optional arguments.
 * Note that this can be a source of errors: if it is possible that your
 * prompt contains {@code %} characters, you must use the format string {@code "%s"}
 * and pass the actual prompt as a parameter.
 *
 * @param format the format string (see {@link java.util.Formatter#format})
 * @param args
 *            the list of arguments passed to the formatter. If there are
 *            more arguments than required by {@code format},
 *            additional arguments are ignored.
 * @return the line, or null at EOF.
 */
public String readLine(String format, Object... args) {
  synchronized (CONSOLE_LOCK) {
    format(format, args);
    return readLine();
  }
}

代码示例来源:origin: robovm/robovm

/**
 * Reads a password from the console. The password will not be echoed to the display.
 *
 * @return a character array containing the password, or null at EOF.
 */
public char[] readPassword() {
  synchronized (CONSOLE_LOCK) {
    int previousState = setEcho(false, 0);
    try {
      String password = readLine();
      writer.println(); // We won't have echoed the user's newline.
      return (password == null) ? null : password.toCharArray();
    } finally {
      setEcho(true, previousState);
    }
  }
}

代码示例来源:origin: stackoverflow.com

BufferedReader in = new BufferedReader(System.console().reader());
BufferedWriter out = new PrintWriter(System.console().writer(), true);

out.println(in.readLine().toUpperCase());

代码示例来源:origin: stackoverflow.com

BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
  System.out.println("Please enter the value");      
  double a = in.readDouble();
  System.console().writer().println("thank you for entering " + a);

代码示例来源:origin: syncany/syncany

public String readLine() {
  try {
    if (console == null) {
      return getSystemInReader().readLine();
    }
    else {
      return console.readLine();
    }
  }
  catch (Exception e) {
    throw new RuntimeException(e);
  }
}

代码示例来源:origin: stackoverflow.com

public static void main(String args[]) {
  Console console = System.console();
  BufferedReader reader = new BufferedReader(console.reader());
  main2(reader, console.writer(), args);
}

static void main2(BufferedReader reader, Writer writer, String args[] {
  String str = reader.readline();
  System.out.println("halo"+str);
}

代码示例来源:origin: stackoverflow.com

public class ConsoleDemo {
 public static void main(String[] args) {
  String[] data = { "\u250C\u2500\u2500\u2500\u2500\u2500\u2510", 
    "\u2502Hello\u2502",
    "\u2514\u2500\u2500\u2500\u2500\u2500\u2518" };
  for (String s : data) {
   System.out.println(s);
  }
  for (String s : data) {
   System.console().writer().println(s);
  }
 }
}

代码示例来源:origin: io.snappydata/gemfire-hydra-tests

private static void writeToConsole(String s) {
 System.console().writer().write(s);
 System.console().writer().flush();
 System.console().flush();
}

代码示例来源:origin: stackoverflow.com

public class TestUnicode {

public static void main(String[] args) throws IOException
{
Console console = System.console();
String message = console.readLine();
console.writer().println(message);
}

代码示例来源:origin: stackoverflow.com

public static void main(String[] args){
  promptUserForInput(System.console());
}
public static int promptUserForInput(Console io){
  Scanner in = new Scanner(io.reader());
  PrintWriter out = io.writer();
  out.print("Enter an integer:");
  // [...]
}

代码示例来源:origin: syncany/syncany

public char[] readPassword(String fmt, Object... args) {
  if (console == null) {
    getSystemOutWriter().write(String.format(fmt, args));
    return readLine().toCharArray();
  }
  else {
    return console.readPassword(fmt, args);
  }
}

代码示例来源:origin: syncany/syncany

public String readLine(String fmt, Object... args) {
  if (console == null) {
    getSystemOutWriter().write(String.format(fmt, args));
    return readLine();
  }
  else {
    return console.readLine(fmt, args);
  }
}

代码示例来源:origin: neo4j/neo4j

@Override
public char[] promptPassword( String fmt, Object... args )
{
  return console.readPassword( fmt, args );
}

代码示例来源:origin: neo4j/neo4j

@Override
public String promptLine( String fmt, Object... args )
{
  return console.readLine( fmt, args );
}

代码示例来源:origin: hierynomus/sshj

@Override
protected boolean hostKeyUnverifiableAction(String hostname, PublicKey key) {
  final KeyType type = KeyType.fromKey(key);
  console.printf("The authenticity of host '%s' can't be established.\n" +
      "%s key fingerprint is %s.\n", hostname, type, SecurityUtils.getFingerprint(key));
  String response = console.readLine("Are you sure you want to continue connecting (yes/no)? ");
  while (!(response.equalsIgnoreCase(YES) || response.equalsIgnoreCase(NO))) {
    response = console.readLine("Please explicitly enter yes/no: ");
  }
  if (response.equalsIgnoreCase(YES)) {
    try {
      entries().add(new HostEntry(null, hostname, KeyType.fromKey(key), key));
      write();
      console.printf("Warning: Permanently added '%s' (%s) to the list of known hosts.\n", hostname, type);
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
    return true;
  }
  return false;
}

代码示例来源:origin: biezhi/learn-java8

public static void main(String[] args) {

    java.io.Console console = System.console();

    if (console != null) {
      String user = new String(console.readLine(" Enter User: ", new Object[0]));
      String pwd  = new String(console.readPassword(" Enter Password: ", new Object[0]));
      console.printf(" User name is:%s ", new Object[]{user});
      console.printf(" Password is:%s ", new Object[]{pwd});
    } else {
      System.out.println(" No Console! ");
    }

  }
}

相关文章