首頁 > Java > java教程 > 主體

Java控制台

PHPz
發布: 2024-08-30 16:10:15
原創
409 人瀏覽過

Java Console 是 JDK 1.6 中引入的 java.io 套件中的一個類,用於啟動讀取基於字元的條目的控制台裝置。該類別用於讀取使用者的輸入並將其寫入控制台。由於以下 2 個原因,這比緩衝的 Reader 和 Writer 更受歡迎:-

  1. 它很容易訪問,因為它使用 System 類別來呼叫新的 Console 實例,並且 Console 類別沒有自己的建構子。
  2. 它有助於輸入安全憑證,例如螢幕上未顯示的密碼。

文法:

開始您的免費軟體開發課程

網頁開發、程式語言、軟體測試及其他

public final class Console extends Object implements Flushable
登入後複製

控制台類別在 Java 中如何運作?

Console 類別用於從控制台讀取輸入或將輸出寫入其中。由於 Console 類別沒有建構函數,並且使用 System 類別實例化,因此更易於使用。此外,Console 類別以更安全的方式從控制台讀取輸入,因為它可以幫助使用者取得安全憑證等輸入,使其不會顯示在螢幕上。因此,如果需要使用安全憑證,Console 類別非常有用。

Java 控制台的方法

以下是Java Console的方法:

1。 public PrintWriterwriter(): 此方法用於檢索與正在執行的控制台物件關聯的 PrintWriter 的唯一實例。此方法不需要任何參數。

2。 public voidlush():Console 類別提供此方法來刷新控制台實例。如果緩衝區中存在輸出,它也會注意立即列印輸出。呼叫此函數不需要任何參數。

3。 public Reader reader(): 此方法給出與目前控制台關聯的 Reader 類別的唯一實例。 Reader 類別的實例作為輸入給出,用於從控制台讀取字元。

4。 public Console format( StringformatVar, Object …ages): 此方法有助於使用指定的格式字串和參數在控制台輸出流上發送給定的格式化字串。

參數:

  • formatVar: 需要使用指定的字串和參數在控制台輸出上列印格式字串。
  • args: 這些參數由 formatVar 中傳遞的字串中的格式說明符引用。

控制台實例作為此函數的輸出發送,並列印格式化字串。如果指定的格式沒有正確的語法,此方法將拋出 IllegalFormatException。

5。 public Console printf( StringformatVar, Object … agrs): 此方法有助於使用控制台畫面上傳遞的指定格式參數列印格式化字串。

  • formatVar – 格式字串需要使用指定的字串和參數列印在控制台輸出上。
  • args – 這些參數由 formatVar 中傳遞的字串中的格式說明符引用。

控制台實例作為此函數的輸出發送,並列印格式化字串。如果指定的格式沒有正確的語法,此方法將拋出 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): 此方法用於在螢幕上顯示格式化提示,該提示使用安全模式讀取字符,這樣當我們鍵入時它就不會顯示在螢幕上。

  • formatVar: The format string needs to be printed on the console output using the specified string and arguments.
  • args: These arguments are referred to by the format specifiers in the string passed in formatVar.

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.

Examples to Implement of Java Console

Below are the examples of Java Console:

Example #1

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:

Java控制台

Example #2

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:

Java控制台

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”);
登入後複製

Java控制台

Note: Console program must be run using command line since eclipse gives exception while running this program.

Conclusion

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中文網其他相關文章!

相關標籤:
來源:php
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!