目录
控制台类在 Java 中如何工作?
Java 控制台的方法
Examples to Implement of Java Console
Example #2
Conclusion
首页 Java java教程 Java控制台

Java控制台

Aug 30, 2024 pm 04:10 PM
java

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控制台的方法:

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中文网其他相关文章!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

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

AI Clothes Remover

AI Clothes Remover

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

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

<🎜>:泡泡胶模拟器无穷大 - 如何获取和使用皇家钥匙
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
Mandragora:巫婆树的耳语 - 如何解锁抓钩
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
北端:融合系统,解释
3 周前 By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

热门话题

Java教程
1667
14
CakePHP 教程
1426
52
Laravel 教程
1328
25
PHP教程
1273
29
C# 教程
1255
24
PHP:网络开发的关键语言 PHP:网络开发的关键语言 Apr 13, 2025 am 12:08 AM

PHP是一种广泛应用于服务器端的脚本语言,特别适合web开发。1.PHP可以嵌入HTML,处理HTTP请求和响应,支持多种数据库。2.PHP用于生成动态网页内容,处理表单数据,访问数据库等,具有强大的社区支持和开源资源。3.PHP是解释型语言,执行过程包括词法分析、语法分析、编译和执行。4.PHP可以与MySQL结合用于用户注册系统等高级应用。5.调试PHP时,可使用error_reporting()和var_dump()等函数。6.优化PHP代码可通过缓存机制、优化数据库查询和使用内置函数。7

突破或从Java 8流返回? 突破或从Java 8流返回? Feb 07, 2025 pm 12:09 PM

Java 8引入了Stream API,提供了一种强大且表达力丰富的处理数据集合的方式。然而,使用Stream时,一个常见问题是:如何从forEach操作中中断或返回? 传统循环允许提前中断或返回,但Stream的forEach方法并不直接支持这种方式。本文将解释原因,并探讨在Stream处理系统中实现提前终止的替代方法。 延伸阅读: Java Stream API改进 理解Stream forEach forEach方法是一个终端操作,它对Stream中的每个元素执行一个操作。它的设计意图是处

PHP与Python:了解差异 PHP与Python:了解差异 Apr 11, 2025 am 12:15 AM

PHP和Python各有优势,选择应基于项目需求。1.PHP适合web开发,语法简单,执行效率高。2.Python适用于数据科学和机器学习,语法简洁,库丰富。

PHP与其他语言:比较 PHP与其他语言:比较 Apr 13, 2025 am 12:19 AM

PHP适合web开发,特别是在快速开发和处理动态内容方面表现出色,但不擅长数据科学和企业级应用。与Python相比,PHP在web开发中更具优势,但在数据科学领域不如Python;与Java相比,PHP在企业级应用中表现较差,但在web开发中更灵活;与JavaScript相比,PHP在后端开发中更简洁,但在前端开发中不如JavaScript。

PHP与Python:核心功能 PHP与Python:核心功能 Apr 13, 2025 am 12:16 AM

PHP和Python各有优势,适合不同场景。1.PHP适用于web开发,提供内置web服务器和丰富函数库。2.Python适合数据科学和机器学习,语法简洁且有强大标准库。选择时应根据项目需求决定。

PHP的影响:网络开发及以后 PHP的影响:网络开发及以后 Apr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP:许多网站的基础 PHP:许多网站的基础 Apr 13, 2025 am 12:07 AM

PHP成为许多网站首选技术栈的原因包括其易用性、强大社区支持和广泛应用。1)易于学习和使用,适合初学者。2)拥有庞大的开发者社区,资源丰富。3)广泛应用于WordPress、Drupal等平台。4)与Web服务器紧密集成,简化开发部署。

PHP与Python:用例和应用程序 PHP与Python:用例和应用程序 Apr 17, 2025 am 12:23 AM

PHP适用于Web开发和内容管理系统,Python适合数据科学、机器学习和自动化脚本。1.PHP在构建快速、可扩展的网站和应用程序方面表现出色,常用于WordPress等CMS。2.Python在数据科学和机器学习领域表现卓越,拥有丰富的库如NumPy和TensorFlow。

See all articles