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控制台的方法:
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树本质上是带了平衡功能的二叉查找树,下面一起来看一下,希望对大家有帮助。

封装是一种信息隐藏技术,是指一种将抽象性函式接口的实现细节部分包装、隐藏起来的方法;封装可以被认为是一个保护屏障,防止指定类的代码和数据被外部类定义的代码随机访问。封装可以通过关键字private,protected和public实现。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

Atom编辑器mac版下载
最流行的的开源编辑器

Dreamweaver Mac版
视觉化网页开发工具

VSCode Windows 64位 下载
微软推出的免费、功能强大的一款IDE编辑器

适用于 Eclipse 的 SAP NetWeaver 服务器适配器
将Eclipse与SAP NetWeaver应用服务器集成。

EditPlus 中文破解版
体积小,语法高亮,不支持代码提示功能