Whether a virtual machine has a console 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 exist and will typically be connected to the keyboard and display from which the virtual machine was launched. If the virtual machine is started automatically, for example by a background job scheduler, then it may not have a console.
If this virtual machine has a console then it is represented by a
unique instance of this class which can be obtained by invoking the
java.
method. If no console device is
available then an invocation of 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.
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.
Console cons; char[] passwd; if ((cons = System.console()) != null && (passwd = cons.readPassword("[%s]", "Password:")) != null) { ... java.util.Arrays.fill(passwd, ' '); }
Modifier and Type | Field and Description |
---|---|
pack-priv static final Charset | |
private static final Console | |
private static final boolean |
Access | Constructor and Description |
---|---|
pack-priv |
Modifier and Type | Method and Description |
---|---|
public Charset | |
private static native String | |
public void | flush()
Implements java. Flushes the console and forces any buffered output to be written immediately. |
public Console | Returns: This consoleA format string as described in format, Object... Format string syntax .Arguments referenced by the format specifiers in the format
string. If there are more arguments than format specifiers, the
extra arguments are ignored. The number of arguments is
variable and may be zero. The maximum number of arguments is
limited by the maximum dimension of a Java array as defined by
The Java Virtual Machine Specification.
The behavior on a
args)null argument depends on the conversion .Writes a formatted string to this console's output stream using
the specified format string and arguments with the
|
public Console | Returns: This consoleA format string as described in format, Object... Format string syntax .Arguments referenced by the format specifiers in the format
string. If there are more arguments than format specifiers, the
extra arguments are ignored. The number of arguments is
variable and may be zero. The maximum number of arguments is
limited by the maximum dimension of a Java array as defined by
The Java Virtual Machine Specification.
The behavior on a
args)null argument depends on the conversion .Writes a formatted string to this console's output stream using
the specified format string and arguments with the specified
|
private static Console | |
public boolean | Returns: true if the Console instance is a terminalReturns |
private static native boolean | |
private static UnsupportedOperationException | |
public Console | Returns: This consoleAn object whose string representation is to be written,
may be obj)null .
Preview
Third Preview of Implicitly Declared Classes and Instance Main Methods (JEP 477).
Writes a string representation of the specified object to this console's output stream and then flushes the console. |
public Console | Returns: This consoleA format string as described in format, Object... Format string syntax .Arguments referenced by the format specifiers in the format
string. If there are more arguments than format specifiers, the
extra arguments are ignored. The number of arguments is
variable and may be zero. The maximum number of arguments is
limited by the maximum dimension of a Java array as defined by
The Java Virtual Machine Specification.
The behavior on a
args)null argument depends on the conversion .A convenience method to write a formatted string to this console's
output stream using the specified format string and arguments with
the |
public Console | Returns: This consoleA format string as described in format, Object... Format string syntax .Arguments referenced by the format specifiers in the format
string. If there are more arguments than format specifiers, the
extra arguments are ignored. The number of arguments is
variable and may be zero. The maximum number of arguments is
limited by the maximum dimension of a Java array as defined by
The Java Virtual Machine Specification.
The behavior on a
args)null argument depends on the conversion .A convenience method to write a formatted string to this console's
output stream using the specified format string and arguments with
the specified |
public Console | Returns: This consoleAn object whose string representation is to be written,
may be obj)null .
Preview
Third Preview of Implicitly Declared Classes and Instance Main Methods (JEP 477).
Writes a string representation of the specified object to this console's
output stream, terminates the line using |
public Reader | |
public String | Returns: A string containing the line read from the console, not including any line-termination characters, ornull
if an end of stream has been reached.A format string as described in format, Object... Format string syntax .Arguments referenced by the format specifiers in the format
string. If there are more arguments than format specifiers, the
extra arguments are ignored. The number of arguments is
variable and may be zero. The maximum number of arguments is
limited by the maximum dimension of a Java array as defined by
The Java Virtual Machine Specification.
The behavior on a
args)null argument depends on the conversion .Provides a formatted prompt using the
|
public String | Returns: A string containing the line read from the console, not including any line-termination characters, ornull
if an end of stream has been reached.A format string as described in format, Object... Format string syntax .Arguments referenced by the format specifiers in the format
string. If there are more arguments than format specifiers, the
extra arguments are ignored. The number of arguments is
variable and may be zero. The maximum number of arguments is
limited by the maximum dimension of a Java array as defined by
The Java Virtual Machine Specification.
The behavior on a
args)null argument depends on the conversion .Provides a formatted prompt using the specified |
public String | Returns: A string containing the line read from the console, not including any line-termination characters, ornull
if an end of stream has been reached.Reads a single line of text from the console. |
public String | Returns: A string containing the line read from the console, not including any line-termination characters, ornull
if an end of stream has been reached without having read
any characters.A prompt string, may be prompt)null .
Preview
Third Preview of Implicitly Declared Classes and Instance Main Methods (JEP 477).
Writes a prompt as if by calling |
public char[] | Returns: A character array containing the password or passphrase read from the console, not including any line-termination characters, ornull if an end of stream has been reached.A format string as described in format, Object... Format string syntax
for the prompt text.Arguments referenced by the format specifiers in the format
string. If there are more arguments than format specifiers, the
extra arguments are ignored. The number of arguments is
variable and may be zero. The maximum number of arguments is
limited by the maximum dimension of a Java array as defined by
The Java Virtual Machine Specification.
The behavior on a
args)null argument depends on the conversion .Provides a formatted prompt using the
|
public char[] | Returns: A character array containing the password or passphrase read from the console, not including any line-termination characters, ornull if an end of stream has been reached.A format string as described in format, Object... Format string syntax
for the prompt text.Arguments referenced by the format specifiers in the format
string. If there are more arguments than format specifiers, the
extra arguments are ignored. The number of arguments is
variable and may be zero. The maximum number of arguments is
limited by the maximum dimension of a Java array as defined by
The Java Virtual Machine Specification.
The behavior on a
args)null argument depends on the conversion .Provides a formatted prompt using the specified |
public char[] | Returns: A character array containing the password or passphrase read from the console, not including any line-termination characters, ornull if an end of stream has been reached.Reads a password or passphrase from the console with echoing disabled. |
public PrintWriter | Returns: The printwriter associated with this consoleRetrieves the unique |
CHARSET | back to summary |
---|---|
pack-priv static final Charset CHARSET |
cons | back to summary |
---|---|
private static final Console cons |
istty | back to summary |
---|---|
private static final boolean istty |
Console | back to summary |
---|---|
pack-priv Console() Package private no-arg constructor. |
charset | back to summary |
---|---|
public Charset charset() Returns the
The returned charset corresponds to the input and output source
(e.g., keyboard and/or display) specified by the host environment or user.
It may not necessarily be the same as the default charset returned from
|
encoding | back to summary |
---|---|
private static native String encoding() |
flush | back to summary |
---|---|
public void flush() Implements java. Flushes the console and forces any buffered output to be written immediately. |
format | back to summary |
---|---|
public Console format(String format, Object... args) Writes a formatted string to this console's output stream using
the specified format string and arguments with the
|
format | back to summary |
---|---|
public Console format(Locale locale, String format, Object... args) Writes a formatted string to this console's output stream using
the specified format string and arguments with the specified
|
instantiateConsole | back to summary |
---|---|
private static Console instantiateConsole()
|
isTerminal | back to summary |
---|---|
public boolean isTerminal() Returns
This method returns Implementation Note The default implementation returns the value equivalent to calling
|
istty | back to summary |
---|---|
private static native boolean istty() |
newUnsupportedOperationException | back to summary |
---|---|
private static UnsupportedOperationException newUnsupportedOperationException() |
back to summary | |
---|---|
public Console print(Object obj)
Preview Third Preview of Implicitly Declared Classes and Instance Main Methods (JEP 477). Programs can only use Writes a string representation of the specified object to this console's output stream and then flushes the console. The string representation of the specified object is obtained as if
by calling
|
printf | back to summary |
---|---|
public Console printf(String format, Object... args) A convenience method to write a formatted string to this console's
output stream using the specified format string and arguments with
the Implementation Specification This is the same as calling
|
printf | back to summary |
---|---|
public Console printf(Locale locale, String format, Object... args) A convenience method to write a formatted string to this console's
output stream using the specified format string and arguments with
the specified Implementation Specification This is the same as calling
|
println | back to summary |
---|---|
public Console println(Object obj)
Preview Third Preview of Implicitly Declared Classes and Instance Main Methods (JEP 477). Programs can only use Writes a string representation of the specified object to this console's
output stream, terminates the line using The string representation of the specified object is obtained as if
by calling
|
reader | back to summary |
---|---|
public Reader reader() Retrieves the unique
This method is intended to be used by sophisticated applications, for
example, a Console con = System.console(); if (con != null) { Scanner sc = new Scanner(con.reader()); ... }
For simple applications requiring only line-oriented reading, use
The bulk read operations
|
readLine | back to summary |
---|---|
public String readLine(String format, Object... args) Provides a formatted prompt using the
|
readLine | back to summary |
---|---|
public String readLine(Locale locale, String format, Object... args) Provides a formatted prompt using the specified
|
readLine | back to summary |
---|---|
public String readLine() Reads a single line of text from the console. |
readln | back to summary |
---|---|
public String readln(String prompt)
Preview Third Preview of Implicitly Declared Classes and Instance Main Methods (JEP 477). Programs can only use Writes a prompt as if by calling
|
readPassword | back to summary |
---|---|
public char[] readPassword(String format, Object... args) Provides a formatted prompt using the
|
readPassword | back to summary |
---|---|
public char[] readPassword(Locale locale, String format, Object... args) Provides a formatted prompt using the specified
|
readPassword | back to summary |
---|---|
public char[] readPassword() Reads a password or passphrase from the console with echoing disabled.
|
writer | back to summary |
---|---|
public PrintWriter writer() Retrieves the unique
|