← 返回首页
Console (Java SE 26 & JDK 26)
JavaScript is disabled on your browser.
Contents  
  1. Description
  2. Method Summary
  3. Method Details
    1. writer()
    2. reader()
    3. format(String, Object...)
    4. format(Locale, String, Object...)
    5. printf(String, Object...)
    6. printf(Locale, String, Object...)
    7. readLine(String, Object...)
    8. readLine(Locale, String, Object...)
    9. readLine()
    10. readPassword(String, Object...)
    11. readPassword(Locale, String, Object...)
    12. readPassword()
    13. flush()
    14. charset()
    15. isTerminal()
Hide sidebar  Show sidebar

Class Console

java.lang.Object
java.io.Console
All Implemented Interfaces: Flushable
public sealed class Console extends Object implements Flushable
Methods to access the character-based console device, if any, associated with the current Java virtual machine.

Whether a virtual machine's console exists is dependent upon the underlying platform and also upon the manner in which the virtual machine is invoked. If the virtual machine is started from an interactive command line without redirecting the standard input and output streams, then its console will generally exist and will be connected to the keyboard and display from which the virtual machine was launched. If the standard input or standard output have been redirected (for example, to a file or to a pipe), or if the virtual machine was started from a background job scheduler, the console will not exist.

If the console exists, then it is represented by a unique instance of this class which can be obtained by invoking the System.console() method. If the console does not exist, that method will return null.

Read and write operations are synchronized to guarantee the atomic completion of critical operations; therefore invoking methods readLine(), readPassword(), format(), printf() as well as the read, format and write operations on the objects returned by reader() and writer() may block in multithreaded scenarios.

Read and write operations use the Charsets specified by stdin.encoding and stdout.encoding, respectively. The Charset used for write operations can also be retrieved using the charset() method. Since Console is intended for interactive use on a terminal, these charsets are typically the same.

Operations that format strings are locale sensitive, using either the specified Locale, or the default format Locale to produce localized formatted strings.

Invoking close() on the objects returned by the reader() and the writer() will not close the underlying stream of those objects.

The console-read methods return null when the end of the console input stream is reached, for example by typing control-D on Unix or control-Z on Windows. Subsequent read operations will succeed if additional characters are later entered on the console's input device.

Unless otherwise specified, passing a null argument to any method in this class will cause a NullPointerException to be thrown.

Security note: If an application needs to read a password or other secure data, it should use readPassword() or readPassword(String, Object...) and manually zero the returned character array after processing to minimize the lifetime of sensitive data in memory.

Copy Console cons; char[] passwd; if ((cons = System.console()) != null && (passwd = cons.readPassword("[%s]", "Password:")) != null) { ... java.util.Arrays.fill(passwd, ' '); }

Since: 1.6

Scripting on this page tracks web page traffic, but does not change the content in any way.