當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java Java.io.Console用法及代碼示例


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.io.Console class in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。