Home  >  Article  >  Java  >  Java Console

Java Console

PHPz
PHPzOriginal
2024-08-30 16:10:15412browse

Java Console is a class present in the java.io package introduced in JDK 1.6 to launch the console device that reads the character-based entries. This class is used to read the user’s input and write it back to the console. This is preferred over buffered Reader and Writer because of below 2 reasons:-

  1. It is easy to access since it uses System class to invoke a new Console instance, and the Console class has no constructors of its own.
  2. It helps to take inputs of security credentials such as passwords that are not displayed on the screen.

Syntax:

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

public final class Console extends Object implements Flushable

How Console Class Work in Java?

Console class is used to read input from the console or write the output to it. Since the Console class has no constructors and is instantiated using System class, that makes it easier to use. Also, the Console class reads the input from the console in a more secure manner because it helps the user take inputs such as security credentials so that it is not displayed on the screen. Thus Console class is very useful in case one needs to work with security credentials.

Methods of Java Console

Below are the methods of Java Console:

1. public PrintWriterwriter(): This method is used to retrieve a unique instance of PrintWriter that is associated with the running console object. There are no parameters required in this method.

2. public void flush(): The Console class provides this method to flush the console instance. It also takes care to print the output immediately if present in the buffer. No parameters are required to call this function.

3. public Reader reader(): This method gives a unique instance of Reader class that is associated with the current console. An instance of Reader class is given as an input to read the characters from the console.

4. public Console format( StringformatVar, Object … ages): This method helps to send the given formatted string on the console output stream using the specified format strings and arguments.

Parameters:

  • 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.

Console instance is sent as an output by this function with formatted string printed. This method throws IllegalFormatException in case the format specified does not have the correct syntax.

5. public Console printf( StringformatVar, Object … agrs): This method helps to print the formatted string with the specified format arguments passed on the console screen.

  • 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.

Console instance is sent as an output by this function with formatted string printed. This method throws IllegalFormatException in case the format specified does not have the correct syntax.

6. public String readLine( StringformatVar, Object … ages): This method is used to display a formatted prompt to the screen and reads a single line input from the user.

The single line that has been read by the prompt from the console excluding any line-ending characters is returned by the method. In case if nothing is entered, then null is returned by this method. This method throws IllegalFormatException in case the format specified does not have the correct syntax. Also, IOError occurs for any I/O errors.

7. public String readLine(): This method is used to read a single line input from the user from the console, excluding any escape characters if passed.

This String is returned by the method or null in case the end-of-line is reached. No parameters are required in this method as no prompt is to be displayed. While using this method, IOERROR occurs for any I/O errors.

8. public char[] readPassword( String formatVar, Object … ages): This method is used to display a formatted prompt to the screen that reads a character using secure mode such that it is not displayed on the screen as we type.

  • 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 Console

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 Console

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 Console

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.

The above is the detailed content of Java Console. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Java Scanner ClassNext article:Java Scanner Class