Package java.io
Class Console
java.lang.Object
java.io.Console
- All Implemented Interfaces:
Flushable
public final class Console extends Object implements Flushable
Provides access to the console, if available. The system-wide instance can
be accessed via
System.console().- Since:
- 1.6
-
Method Summary
Modifier and Type Method Description voidflush()Flushes the object by writing out any buffered data to the underlying output.Consoleformat(String format, Object... args)Writes a formatted string to the console using the specified format string and arguments.static ConsolegetConsole()Secret accessor forSystem.console.Consoleprintf(String format, Object... args)Equivalent toformat(format, args).Readerreader()Returns theReaderassociated with this console.StringreadLine()Reads a line from the console.StringreadLine(String format, Object... args)Reads a line from this console, using the specified prompt.char[]readPassword()Reads a password from the console.char[]readPassword(String format, Object... args)Reads a password from the console.PrintWriterwriter()Returns theWriterassociated with this console.
-
Method Details
-
getConsole
Secret accessor forSystem.console. -
flush
public void flush()Description copied from interface:FlushableFlushes the object by writing out any buffered data to the underlying output. -
format
Writes a formatted string to the console using the specified format string and arguments.- Parameters:
format- the format string (seeFormatter.format(java.lang.String, java.lang.Object...))args- the list of arguments passed to the formatter. If there are more arguments than required byformat, additional arguments are ignored.- Returns:
- the console instance.
-
printf
Equivalent toformat(format, args). -
reader
Returns theReaderassociated with this console. -
readLine
Reads a line from the console.- Returns:
- the line, or null at EOF.
-
readLine
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%characters, you must use the format string"%s"and pass the actual prompt as a parameter.- Parameters:
format- the format string (seeFormatter.format(java.lang.String, java.lang.Object...))args- the list of arguments passed to the formatter. If there are more arguments than required byformat, additional arguments are ignored.- Returns:
- the line, or null at EOF.
-
readPassword
public char[] readPassword()Reads a password from the console. The password will not be echoed to the display.- Returns:
- a character array containing the password, or null at EOF.
-
readPassword
Reads a password from the console. The password will not be echoed to the display. A formatted prompt is also displayed.- Parameters:
format- the format string (seeFormatter.format(java.lang.String, java.lang.Object...))args- the list of arguments passed to the formatter. If there are more arguments than required byformat, additional arguments are ignored.- Returns:
- a character array containing the password, or null at EOF.
-
writer
Returns theWriterassociated with this console.
-