×

Java Console

If there is a character-based console device connected to the active Java virtual machine, it can be accessed using methods provided by the Java.io.Console class. JDK 6 adds the Console class to the java.io API.

To obtain console input, utilise the Java Console class. It offers ways to read text messages and passwords.

The user won't see the password if you read it using the Console class.

Internally, the system console is connected to the java.io.Console class. Since 1.5, the Console class has been available.

Here is a straightforward example of reading text from a console.

String text=System.console().readLine();    
System.out.println("Text is: "+text);    

Important Information

  • If there is a console, it is used to read from and write to it.
  • Because System.in and System.out provide access to the majority of Console's capabilities, it serves mostly as a convenience class. However, when reading strings from the console, its use can make some console interactions simpler.
  • Constructors are not provided by Console. Instead, using System.console() returns a Console object, as demonstrated here:
 static  Console console( )

A reference to the console is returned if one is present. Null is returned if not. In some circumstances, a console won't be accessible. Therefore, no console I/O is possible if null is returned.

  • It offers techniques for reading text and passwords. The user won't see the password if you read it using the Console class. Internally, the system console is connected to the java.io.Console class.

Java Console class declaration

Let's see the declaration for Java.io.Console class:

public final class Console extends Object implements Flushable 

Java Console class methods

MethodDescription
Reader reader()It is utilised to retrieve the console's related reader object.
String readLine()A single line of text from the console is read using this method.
String readLine(String fmt, Object... args)A formatted prompt is given before the console's single line of text is read.
char[] readPassword()It can read passwords that are not visible on the console.
char[] readPassword(String fmt, Object... args)The formatted prompt is given, and after that, the console reads the password that isn't being displayed.
Console format(String fmt, Object... args)A prepared string can be written with it. to the output stream on the terminal.
Console printf(String format, Object... args)It is employed to write a string to the output stream of the console.
PrintWriter writer()To retrieve the PrintWriter, use this. a piece connected to the console.
void flush()The console is flushed using it.

How to obtain the console's object

The static function console() of the System class returns a single instance of the Console class.

public static Console console(){}   

Let's see the code to get the instance of Console class.

Console c1= System .console();  

Program for Java Console

import java . io . Console ;  
class Read_string {    
public static void main ( String args [ ] ) {    
Console c1 = System . console ( ) ;    
System . out . println ( " Enter your name: " ) ;    
String name = c1 . readLine ( ) ;    
System . out . println ( " Hello "+ name ) ;    
}    
}  

Output

Java Console

Program for Java Console for reading password

import java . io . Console ;  
class  {    
public static void main ( String args [ ] ) {    
Console c1 = System . console ( ) ;    
System . out . println ( " Enter password: " ) ;    
char [ ] character = c1 . readPassword ( ) ;    
String password = String . valueOf ( character ) ;
System . out . println ( " Password is: "+ password ) ;    
}    
}  

Output

Java Console

Related Topics

Java.lang.Exception.NoRunnableMethods

In Programming language, the java lang unexpected no precompiled methods error generally refers to a Junit exception that happens whenever Junit is incapable of locate the precompiled test methods. When...

4 minutes read.

Java Program to remove duplicate characters in a string

In strings, we can find many characters present one or more times in a string; accessing a particular character is difficult due to repetition. Duplicate characters will present in the...

5 minutes read.

How to Convert long to String in Java

How to Convert long to String in java It is used if we have to display a long number in the text field because everything is displayed as a String. A...

3 minutes read.

Bounded buffer problem in Java

The Bounded buffer Problem can also be called a Producer consumer problem. The problem covers two processes—the producer and the consumer—that share a single, fixed-size buffer that serves as a...

4 minutes read.

Maximum length of string in java

In java, String can act as a data type and a class. The string can be defined as the collection of characters that are enclosed with double quotes(“ “). The...

3 minutes read.

Tetris Game in Java

The Tetris game is among the most well-known video games ever produced for computers. Today, we may engage in this game on a mobile device as well. Alexey Pajitnov conceptualized...

12 minutes read.

Difference Between Data Hiding and Abstraction in Java

Abstraction: Data Abstraction and Data Hiding ideas are utilised to show the expected data to the end client and conceal the superfluous subtleties, however, for specific purposes like decreasing the framework's...

10 minutes read.

Java Enum Keyword

Definition: A data type in Java called Enum has a respect to supply of constants. The weekdays (SUN, MON, TUE, WED, THU, FRI, and SAT), directions (NORTH, SOUTH, EAST, and WEST),...

4 minutes read.

Determine the Upper Bound of a Two-Dimensional Array in Java

Multi-Dimensional Array Multi-faceted exhibits in Java are regular and can be named various clusters. Information in a two-layered bunch in Java is put away in 2D even structure. A two-layered collection...

3 minutes read.

Java Solid Principles

Java implements the object-oriented SOLID principles for the design of software architecture. Solid Principles Java implements the object-oriented SOLID principles for the design of software architecture.   Five guiding principles transformed...

4 minutes read.

TreeSet in Java

Java TreeSet with Example Java TreeSet implements the Navigable Set interface. It stores the objects in ascending order. It contains unique elements. Access and retrieval time is fast. It does not...

8 minutes read.

Convert JSON File to String in Java

Before understanding the conversion of JSON file to string, one must know about JSON. What is JSON? JSON stands for JavaScript Object Notation. It is an open standard format lightweight, text-based, and...

3 minutes read.

Java Open File

Java Desktop class give an open() strategy to open a filr. It has a place with a java.awt package. Work area execution is stage subordinate, so it is important to...

5 minutes read.

Polygonal Number in Java

What does a Java Polygonal Number Mean? In mathematics, a polygonal number refers to a number that is expressed through dots or pebbles arranged in a regular polygonal pattern. Alphas are...

4 minutes read.

Polymorphism Program in Java

Polymorphism Program in Java: Polymorphism means the existence of different forms of an object. The object can be a class object or a real-time entity. For example, a person can...

5 minutes read.

How to set path in Java

To make programs that can run on our systems, we need to install programming language-related software in our systems. Different programming languages require different types of software, aka IDEs (Integrated Development...

5 minutes read.

How to download Eclipse for Java

Introduction: We write a java program, and when we want to run it, we need software to run it. Eclipse is a kind of software used to execute a JavaFX...

3 minutes read.

Exception Handling in Java

Before looking at how exceptions are handled in java, it is necessary to see what an exception is. What is Exception? Whenever a program is written, errors are encountered. Some of these...

6 minutes read.

How to Set Java_home in Linux

To set the Java_home in Linux, we must follow several steps to make us understand it easily.  Java follows the principle called WORA (write once run anywhere). We know that java...

3 minutes read.

Java logger

Logging is a crucial Java feature that aids developers in identifying issues. The logging methodology is included with Java as the programming language. It offers the Logging API, which debuted...

7 minutes read.