爪哇。伊奥。Console类提供了访问与当前Java虚拟机关联的基于字符的控制台设备(如果有的话)的方法。控制台类被添加到java中。JDK 6的io。
null
要点:
- 如果控制台存在,它用于读取和写入控制台。
- Console主要是一个便利类,因为它的大部分功能都可以通过系统获得。在和系统中。出来然而,它的使用可以简化某些类型的控制台交互,尤其是在从控制台读取字符串时。
- 控制台不提供构造函数。相反,控制台对象是通过调用系统获得的。控制台(),如下所示:
static Console console( )
如果控制台可用,则返回对它的引用。否则,返回null。控制台并非在所有情况下都可用。因此,如果返回null,则不可能进行控制台I/O。
- 它提供了读取文本和密码的方法。如果使用Console类读取密码,它将不会显示给用户。爪哇。伊奥。Console类在内部与系统控制台连接。
重要方法:
- 作者: 检索与此控制台关联的唯一PrintWriter对象。 语法:
public PrintWriter writer() Returns: The printwriter associated with this console
- 读者: 检索与此控制台关联的唯一读取器对象。 语法:
public Reader reader() Returns: The reader associated with this console
- 格式: 使用指定的格式字符串和参数将格式化字符串写入此控制台的输出流。 语法:
public Console format(String fmt, Object... args) Parameters: 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:This console Throws: IllegalFormatException
- printf: 使用指定的格式字符串和参数将格式化字符串写入此控制台的输出流的方便方法。 语法:
public Console printf(String format, Object... args) Parameters: 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. Returns:This console Throws:IllegalFormatException
- 阅读线: 提供格式化提示,然后从控制台读取单行文本。 语法:
public String readLine(String fmt,Object... args) Parameters: 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.
- 阅读线: 从控制台读取一行文本。 语法:
public String readLine() 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: IOError
- 读取密码: 提供格式化提示,然后在禁用回显的情况下从控制台读取密码或密码短语。 语法:
public char[] readPassword(String fmt,Object... args) Parameters: 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
- 读取密码: 在禁用回显的情况下从控制台读取密码或密码短语 语法:
public char[] readPassword() 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:IOError
- 同花顺: 刷新控制台并强制立即写入任何缓冲输出。 语法:
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" , 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
注:系统。console()在联机IDE中返回null
本文由 尼森特·夏尔马 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 贡献极客。组织 或者把你的文章寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。
如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END