Java Console 是 JDK 1.6 中引入的 java.io 套件中的一個類,用於啟動讀取基於字元的條目的控制台裝置。該類別用於讀取使用者的輸入並將其寫入控制台。由於以下 2 個原因,這比緩衝的 Reader 和 Writer 更受歡迎:-
文法:
開始您的免費軟體開發課程
網頁開發、程式語言、軟體測試及其他
public final class Console extends Object implements Flushable
Console 類別用於從控制台讀取輸入或將輸出寫入其中。由於 Console 類別沒有建構函數,並且使用 System 類別實例化,因此更易於使用。此外,Console 類別以更安全的方式從控制台讀取輸入,因為它可以幫助使用者取得安全憑證等輸入,使其不會顯示在螢幕上。因此,如果需要使用安全憑證,Console 類別非常有用。
以下是Java Console的方法:
1。 public PrintWriterwriter(): 此方法用於檢索與正在執行的控制台物件關聯的 PrintWriter 的唯一實例。此方法不需要任何參數。
2。 public voidlush():Console 類別提供此方法來刷新控制台實例。如果緩衝區中存在輸出,它也會注意立即列印輸出。呼叫此函數不需要任何參數。
3。 public Reader reader(): 此方法給出與目前控制台關聯的 Reader 類別的唯一實例。 Reader 類別的實例作為輸入給出,用於從控制台讀取字元。
4。 public Console format( StringformatVar, Object …ages): 此方法有助於使用指定的格式字串和參數在控制台輸出流上發送給定的格式化字串。
參數:
控制台實例作為此函數的輸出發送,並列印格式化字串。如果指定的格式沒有正確的語法,此方法將拋出 IllegalFormatException。
5。 public Console printf( StringformatVar, Object … agrs): 此方法有助於使用控制台畫面上傳遞的指定格式參數列印格式化字串。
控制台實例作為此函數的輸出發送,並列印格式化字串。如果指定的格式沒有正確的語法,此方法將拋出 IllegalFormatException。
6。 public String readLine( StringformatVar, Object …ages): 此方法用於在螢幕上顯示格式化提示並讀取使用者輸入的單行內容。
此方法傳回從控制台提示字元讀取的單行,不包括任何行結束字元。如果沒有輸入任何內容,則此方法將傳回 null。如果指定的格式沒有正確的語法,則此方法將引發 IllegalFormatException。此外,任何 I/O 錯誤都會發生 IOError。
7。 public String readLine(): 此方法用於從控制台讀取使用者輸入的單行內容,不包含任何傳遞的轉義字元。
該方法傳回此字串,如果到達行尾,則傳回 null。此方法不需要任何參數,因為不會顯示提示。使用此方法時,任何 I/O 錯誤都會發生 IOERROR。
8。 public char[] readPassword( String formatVar, Object …ages): 此方法用於在螢幕上顯示格式化提示,該提示使用安全模式讀取字符,這樣當我們鍵入時它就不會顯示在螢幕上。
Character Array containing the password is sent as output by this function or null in case the end-of-line is reached. The returned character array excludes line-termination characters. This method throws IllegalFormatException in case the format specified does not have the correct syntax. This method IOERROR occurs for any I/O errors.
9. public char[] readPassword(): This method is used to read password string from the console in a secure mode without displaying any prompt. There are no parameters that need to be passed. The string of characters containing the password is returned excluding the line-termination characters or null in case the end-of-line is reached.
Code:
import java.io.Console; public class ConsolePrac { public static void main(String[] args) { Console consoleObj = null; try { consoleObj = System.console(); if (consoleObj != null) { String person = consoleObj.readLine("Please enter your Name: "); System.out.println("Welcome " + person); System.out.println("Please check the below result"); String fmt = "%2$8s %1$10s %3$3s%n"; consoleObj.format(fmt, "Name", "RollNo", "MArks"); consoleObj.format(fmt, "-----", "-----", "-----"); consoleObj.format(fmt, "Raj", "1212", "75"); consoleObj.printf(fmt, "Meeta", "1213", "67"); consoleObj.printf(fmt, "Sanjana", "1215", "89"); consoleObj.printf(fmt, "Pawan", "1214", "80"); } consoleObj.flush(); } catch(Exception ex) { ex.printStackTrace(); } } }
Output:
Code:
import java.io.Console import java.util.Scanner; import java.io.PrintWriter; public class ConsolePrac { public static void main(String[] args) { Console consoleObj = null; PrintWriter PWObj = null; String fmt1 = "%1$6s %2$5s %3$10s%n"; String fmt2 = "%1$8s %2$10s %3$5s %4$5s %5$10s%n"; Scanner scanObj = null; try { consoleObj = System.console(); if (consoleObj != null) { System.out.print("Please enter your Name: "); scanObj = new Scanner(consoleObj.reader()); String person = scanObj.next(); String empID = consoleObj.readLine(fmt1, "Please","enter","EmployeeID: "); char[] pwd = consoleObj.readPassword("Please enter your Password: "); char[] ans1 = consoleObj.readPassword(fmt2,"Security","question- ","Enter","your","Mothers'name: "); PWObj = consoleObj.writer(); PWObj.println("Welcome "+person +" "+empID); } } catch(Exception ex) { ex.printStackTrace(); } } }
Output:
Note: The format string’s arguments must be the same as the String to be displayed. Please see below 2 scenarios:Case 1: The arguments are more than specified in the format string.
String fmt= "%1$30s %2$10s" String empID = consoleObj.readLine(fmt1, "Please","enter","your","EmployeeID: ");
In the above case, only the first 2 strings will be printed, and others are ignored.
Case 2: In case the arguments specified are less than a missing argument exception is thrown.
String fmt= "%1$30s %2$10s" ,"%3$10s%n" String empID = consoleObj.readLine(fmt1, "Please","enter”);
Console class is one of the great utility introduced with JDK 1.6 that helps to read the input from a console instance in a secure manner. It also provides methods to write output to the console screen.
以上是Java控制台的詳細內容。更多資訊請關注PHP中文網其他相關文章!