首页  >  文章  >  Java  >  Java控制台

Java控制台

PHPz
PHPz原创
2024-08-30 16:10:15412浏览

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
上一篇:Java Scanner Class下一篇:Java Runtime class