Java Console
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:-
- 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.
- 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:
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.
The above is the detailed content of Java Console. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Guide to Square Root in Java. Here we discuss how Square Root works in Java with example and its code implementation respectively.

Guide to Perfect Number in Java. Here we discuss the Definition, How to check Perfect number in Java?, examples with code implementation.

Guide to Random Number Generator in Java. Here we discuss Functions in Java with examples and two different Generators with ther examples.

Guide to Weka in Java. Here we discuss the Introduction, how to use weka java, the type of platform, and advantages with examples.

Guide to Smith Number in Java. Here we discuss the Definition, How to check smith number in Java? example with code implementation.

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

Guide to TimeStamp to Date in Java. Here we also discuss the introduction and how to convert timestamp to date in java along with examples.
