Java Console 是 JDK 1.6 中引入的 java.io 套件中的一個類,用於啟動讀取基於字元的條目的控制台裝置。該類別用於讀取使用者的輸入並將其寫入控制台。由於以下 2 個原因,這比緩衝的 Reader 和 Writer 更受歡迎:-
- 它很容易訪問,因為它使用 System 類別來呼叫新的 Console 實例,並且 Console 類別沒有自己的建構子。
- 它有助於輸入安全憑證,例如螢幕上未顯示的密碼。
文法:
開始您的免費軟體開發課程
網頁開發、程式語言、軟體測試及其他
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:
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:
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”);
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中文網其他相關文章!

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于结构化数据处理开源库SPL的相关问题,下面就一起来看一下java下理想的结构化数据处理类库,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于PriorityQueue优先级队列的相关知识,Java集合框架中提供了PriorityQueue和PriorityBlockingQueue两种类型的优先级队列,PriorityQueue是线程不安全的,PriorityBlockingQueue是线程安全的,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于java锁的相关问题,包括了独占锁、悲观锁、乐观锁、共享锁等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于多线程的相关问题,包括了线程安装、线程加锁与线程不安全的原因、线程安全的标准类等等内容,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于枚举的相关问题,包括了枚举的基本操作、集合类对枚举的支持等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Java的相关知识,其中主要介绍了关于关键字中this和super的相关问题,以及他们的一些区别,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于平衡二叉树(AVL树)的相关知识,AVL树本质上是带了平衡功能的二叉查找树,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Java的相关知识,其中主要整理了Stream流的概念和使用的相关问题,包括了Stream流的概念、Stream流的获取、Stream流的常用方法等等内容,下面一起来看一下,希望对大家有帮助。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器

WebStorm Mac版
好用的JavaScript開發工具

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

Atom編輯器mac版下載
最受歡迎的的開源編輯器