Java.io.Console 类提供了访问与当前 Java 虚拟机关联的基于字符的控制台设备(如果有)的方法。 JDK 6 将 Console 类添加到java.io。
要点:
- 它用于读取和写入控制台(如果存在)。
- Console 主要是一个便利类,因为它的大部分函数都可以通过 System.in 和 System.out 获得。但是,它的使用可以简化某些类型的控制台交互,尤其是从控制台读取字符串时。
- 控制台不提供构造函数。相反,Console 对象是通过调用 System.console( ) 获得的,如下所示:
static Console console( )
如果控制台可用,则返回对其的引用。否则,返回 null。控制台并非在所有情况下都可用。因此,如果返回 null,则不可能进行控制台 I/O。
- 它提供了读取文本和密码的方法。如果使用 Console 类读取密码,则不会向用户显示密码。java.io.Console 类内部附加有系统控制台。
重要方法:
- writer : 检索与此控制台关联的唯一PrintWriter对象。
Syntax:
public PrintWriter writer() 返回: The printwriter associated with this console
- reader : 检索与此控制台关联的唯一 Reader 对象。
Syntax:
public Reader reader() Returns: The reader associated with this console
- format :使用指定的格式字符串和参数将格式化字符串写入此控制台的输出流。
Syntax:
public Console format(String fmt, Object... args) 参数: fmt - A format string as described in Format string syntax args - Arguments referenced by the format specifiers in the format string. If there are more arguments than format specifiers, the extra arguments are ignored. 返回:This console Throws: IllegalFormatException
- printf : 使用指定的格式字符串和参数将格式化字符串写入此控制台的输出流的便捷方法。
用法:public Console printf(String format, Object... args) 参数: format - A format string as described in Format string syntax. args - Arguments referenced by the format specifiers in the format string. If there are more arguments than format specifiers, the extra arguments are ignored. 返回:This console Throws:IllegalFormatException
- readLine : 提供格式化提示,然后从控制台读取一行文本。
用法:public String readLine(String fmt,Object... args) 参数: fmt - A format string as described in Format string syntax. args - Arguments referenced by the format specifiers in the format string. If there are more arguments than format specifiers, the extra arguments are ignored. Returns: 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 IOError - If an I/O error occurs.
- readLine :从控制台读取一行文本。
用法:public String readLine() 返回: 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
- readPassword: 提供格式化提示,然后从控制台读取密码或密码短语并禁用回显。
用法:public char[] readPassword(String fmt,Object... args) 参数: fmt - A format string as described in Format string syntax for the prompt text. args - Arguments referenced by the format specifiers in the format string. Returns: 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 IOError
- readPassword :从控制台读取密码或密码短语并禁用回显
用法:public char[] readPassword() 返回: 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
- flush : 刷新控制台并强制立即写入所有缓冲的输出。
用法:public void flush() Specified by: flush in interface Flushable
程序:
// Java Program to demonstrate Console Methods
import java.io.*;
class ConsoleDemo
{
public static void main(String args[])
{
String str;
//Obtaining a reference to the console.
Console con = System.console();
// Checking If there is no console available, then exit.
if(con == null)
{
System.out.print("No console available");
return;
}
// Read a string and then display it.
str = con.readLine("Enter your name: ");
con.printf("Here is your name: %s\n", str);
//to read password and then display it
System.out.println("Enter the password: ");
char[] ch=con.readPassword();
//converting char array into string
String pass = String.valueOf(ch);
System.out.println("Password is: " + pass);
}
}
输出:
Enter your name: Nishant Sharma Here is your name: Nishant Sharma Enter the password: Password is: dada
注意:System.console() 在在线 IDE 中返回 null
相关用法
- Java Java.io.Console.flush()用法及代码示例
- Java Java.io.Console.format()用法及代码示例
- Java Java.io.Console.printf()用法及代码示例
- Java Java.io.Console.reader()用法及代码示例
- Java Java.io.Console.readLine()用法及代码示例
- Java Java.io.Console.readPassword()用法及代码示例
- Java Java.io.Console.writer()用法及代码示例
- Java Java.io.CharArrayReader.close()用法及代码示例
- Java Java.io.CharArrayReader.mark()用法及代码示例
- Java Java.io.CharArrayReader.markSupported()用法及代码示例
- Java Java.io.CharArrayReader.read()用法及代码示例
- Java Java.io.CharArrayReader.ready()用法及代码示例
- Java Java.io.CharArrayReader.reset()用法及代码示例
- Java Java.io.CharArrayReader.skip()用法及代码示例
- Java Java.io.CharArrayWriter.append()用法及代码示例
- Java Java.io.CharArrayWriter.close()用法及代码示例
- Java Java.io.CharArrayWriter.flush()用法及代码示例
- Java Java.io.CharArrayWriter.reset()用法及代码示例
- Java Java.io.CharArrayWriter.toCharArray()用法及代码示例
- Java Java.io.CharArrayWriter.toString()用法及代码示例
- Java Java.io.CharArrayWriter.write()用法及代码示例
- Java Java.io.CharArrayWriter.writeTo()用法及代码示例
- Java Java.io.CharArrayReader用法及代码示例
- Java Java.io.BufferedInputStream.available()用法及代码示例
- Java Java.io.BufferedInputStream.close()用法及代码示例
注:本文由纯净天空筛选整理自佚名大神的英文原创作品 Java.io.Console class in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。