Most visited

Recently visited

Added in API level 9

Console

public final class Console
extends Object implements Flushable

java.lang.Object
   ↳ java.io.Console


用于访问与当前Java虚拟机关联的基于角色的控制台设备(如果有)的方法。

虚拟机是否具有控制台取决于底层平台以及虚拟机的调用方式。 如果虚拟机从交互式命令行启动而不重定向标准输入和输出流,则其控制台将存在,并且通常会连接到启动虚拟机的键盘和显示器。 如果虚拟机是自动启动的,例如通过后台作业调度程序,那么它通常没有控制台。

如果这个虚拟机有一个控制台,那么它就是通过调用console()方法获得的这个类的唯一实例来表示的。 如果没有控制台设备可用,那么对该方法的调用将返回null

读取和写入操作是同步的,以保证关键操作的原子完成; 因此调用方法readLine()readPassword()format()printf()以及读,格式和写在由返回的对象的操作reader()writer()在多线程方案可能阻塞。

reader()writer()返回的对象调用 close()将不会关闭这些对象的基础流。

控制台读取方法在控制台输入流结束时返回null ,例如在Unix上输入control-D或在Windows上输入control-Z。 如果稍后在控制台的输入设备上输入了其他字符,后续的读取操作将会成功。

除非另有说明,否则将 null参数传递给 此类中的任何方法将导致引发 NullPointerException

安全注意事项:如果应用程序需要读取密码或其他安全数据,则应使用 readPassword()readPassword(String, Object)并在处理后手动清零返回的字符数组,以尽量减少内存中敏感数据的使用寿命。

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

Summary

Public methods

void flush()

刷新控制台并强制任何缓冲输出立即写入。

Console format(String fmt, Object... args)

使用指定的格式字符串和参数将格式化的字符串写入此控制台的输出流。

Console printf(String format, Object... args)

使用指定的格式字符串和参数将格式化的字符串写入此控制台的输出流的便捷方法。

String readLine()

从控制台读取一行文本。

String readLine(String fmt, Object... args)

提供格式化的提示,然后从控制台读取一行文本。

char[] readPassword()

从控制台读取禁用回显的密码或密码

char[] readPassword(String fmt, Object... args)

提供格式化提示,然后从控制台读取禁用回显的密码或密码。

Reader reader()

检索与此控制台关联的唯一 Reader对象。

PrintWriter writer()

检索与此控制台关联的唯一 PrintWriter对象。

Inherited methods

From class java.lang.Object
From interface java.io.Flushable

Public methods

flush

Added in API level 9
void flush ()

刷新控制台并强制任何缓冲输出立即写入。

format

Added in API level 9
Console format (String fmt, 
                Object... args)

使用指定的格式字符串和参数将格式化的字符串写入此控制台的输出流。

Parameters
fmt String: A format string as described in Format string syntax
args Object: 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 behaviour on a null argument depends on the conversion.
Returns
Console This console
Throws
IllegalFormatException If a format string contains an illegal syntax, a format specifier that is incompatible with the given arguments, insufficient arguments given the format string, or other illegal conditions. For specification of all possible formatting errors, see the Details section of the formatter class specification.

printf

Added in API level 9
Console printf (String format, 
                Object... args)

使用指定的格式字符串和参数将格式化的字符串写入此控制台的输出流的便捷方法。

对表单 con.printf(format, args)的这种方法的调用与调用方式完全相同

con.format(format, args)
.

Parameters
format String: A format string as described in Format string syntax.
args Object: 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 behaviour on a null argument depends on the conversion.
Returns
Console This console
Throws
IllegalFormatException If a format string contains an illegal syntax, a format specifier that is incompatible with the given arguments, insufficient arguments given the format string, or other illegal conditions. For specification of all possible formatting errors, see the Details section of the formatter class specification.

readLine

Added in API level 9
String readLine ()

从控制台读取一行文本。

Returns
String A string containing the line read from the console, not including any line-termination characters, or null if an end of stream has been reached.
Throws
IOError If an I/O error occurs.

readLine

Added in API level 9
String readLine (String fmt, 
                Object... args)

提供格式化的提示,然后从控制台读取一行文本。

Parameters
fmt String: A format string as described in Format string syntax.
args Object: Arguments referenced by the format specifiers in the format string. If there are more arguments than format specifiers, the extra arguments are ignored. The maximum number of arguments is limited by the maximum dimension of a Java array as defined by The Java™ Virtual Machine Specification.
Returns
String A string containing the line read from the console, not including any line-termination characters, or null if an end of stream has been reached.
Throws
IllegalFormatException If a format string contains an illegal syntax, a format specifier that is incompatible with the given arguments, insufficient arguments given the format string, or other illegal conditions. For specification of all possible formatting errors, see the Details section of the formatter class specification.
IOError If an I/O error occurs.

readPassword

Added in API level 9
char[] readPassword ()

从控制台读取禁用回显的密码或密码

Returns
char[] A character array containing the password or passphrase read from the console, not including any line-termination characters, or null if an end of stream has been reached.
Throws
IOError If an I/O error occurs.

readPassword

Added in API level 9
char[] readPassword (String fmt, 
                Object... args)

提供格式化提示,然后从控制台读取禁用回显的密码或密码。

Parameters
fmt String: A format string as described in Format string syntax for the prompt text.
args Object: Arguments referenced by the format specifiers in the format string. If there are more arguments than format specifiers, the extra arguments are ignored. The maximum number of arguments is limited by the maximum dimension of a Java array as defined by The Java™ Virtual Machine Specification.
Returns
char[] A character array containing the password or passphrase read from the console, not including any line-termination characters, or null if an end of stream has been reached.
Throws
IllegalFormatException If a format string contains an illegal syntax, a format specifier that is incompatible with the given arguments, insufficient arguments given the format string, or other illegal conditions. For specification of all possible formatting errors, see the Details section of the formatter class specification.
IOError If an I/O error occurs.

reader

Added in API level 9
Reader reader ()

检索与此控制台关联的唯一 Reader对象。

此方法旨在供复杂的应用程序使用,例如 Scanner对象,该对象利用 Scanner提供的丰富解析/扫描功能:

 Console con = System.console();
 if (con != null) {
     Scanner sc = new Scanner(con.reader());
     ...
 }
 

对于只需要面向行读取的简单应用,请使用 readLine()

批量读取操作read(char[]) read(char[], int, int) read(java.nio.CharBuffer)对返回的对象不会超出开往每次调用行字符读,即使目标缓冲区有更多字符的空间。 一个行绑定被认为是换行( '\n' ),回车( '\r' ),回车后立即换行或流结束中的任何一个。

Returns
Reader The reader associated with this console

writer

Added in API level 9
PrintWriter writer ()

检索与此控制台关联的唯一 PrintWriter对象。

Returns
PrintWriter The printwriter associated with this console

Hooray!