×

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

Swapping Program in Java

Swapping Program in Java The swapping program in Java is used to interchange the values of the two variables. For example, if X = 12 and Y = 24, then the...

4 minutes read.

Graphics Program in Java

Graphics Program in Java The graphics program in Java is part of the Swing program in Java. In this section, we will learn about the implementation of custom graphics using some...

6 minutes read.

Type Annotations in Java

Only declarations were eligible for annotations in earlier versions of Java. With Java 8, you may now annotate any type use, including types in declarations, generics, and casts: @Encrypted String data; List<@NonNull...

6 minutes read.

Java Xml Parser

In this article, we acknowledge you about what is a XML parser in Java, how is it going to work and what is the purpose of it. Also, the article discusses...

6 minutes read.

Various Operation on Queue using Linked List in Java

We keep track of front and rear pointers in a queue data structure. The head of the line points to the first item, and the back to the last. Rear is...

3 minutes read.

Java Math with Methods and Examples

Java Math class contains various methods for performing math operations like min(), max(), avg() and various trigonometric functions like sin(), cos(), tan() etc. Methods: The java.lang.Math class contains various methods for performing...

5 minutes read.

Scanner in Java

Static way of Programming: When a variable can’t change its value during run time is called a Static way of programming. In this programming a variable is directly assigned to a...

4 minutes read.

Java Variable

The variable is the basic unit of storage in a program. We define a variable using an identifier, a type, and an optional initializer in Java. In Java, variables must be...

4 minutes read.

Java String copyValueOf() method

copyValueOf() method returns a String that holds the character sequence of the character array. Syntax: copyValueOf(char[] data) Parameters: data : the character array i.e. String Returns: It returns a String that contains the characters of the...

2 minutes read.

Best Java IDE

Applications for desktop, workplace, smartphone, and the internet can be created using Java, one of the most popular programming languages. Java will undoubtedly be a popular programming language for so...

5 minutes read.

Sum of digits in string in java

To find the sum of all digits in a string, you need to traverse through the string one by one character; if the character is an integer, you need to...

2 minutes read.

Access modifiers in Java

Access modifiers in Java with Example In Java, there are two types of access modifiers one is a non-access modifier, and other is access modifier. If we talk about access modifier, there...

3 minutes read.

Set Value to Enum in Java

In this article, you will be acknowledged about Enum in java. Most importantly you will learn how to set value to Enum or how to practically customize a value to...

3 minutes read.

Relatively Prime in Java

In this article, you will be very well equipped with a knowledge of a relatively prime number and also Java programs to determine whether a given integer is a relatively...

4 minutes read.

Java Integer class

The Integer class wraps a primitive int type value in an object. Its object contains only a single field whose type is int. Methods: The java.lang.Integer class provides several different methods for...

4 minutes read.

Java Anon Proxy

The Java Anon Proxy (JAP), also known as JonDonym, is a proxy system designed to enable Web browsing with revocable (the use of or publication under a pseudonym, a false...

4 minutes read.

Hybrid Inheritance in Java

The most crucial OOPs concept in Java is inheritance, which enables the transfer of a class's properties to another class. It describes the Is-A relationship generally. We can create a...

3 minutes read.

Java Xmx

This section will explain what Xmx in Java is and how to establish a Java application's maximum heap size. When we execute a Java application, it occasionally displays an error message...

3 minutes read.

MVC in Java

A well-known design pattern is Model-View-Controller. The discipline of web development. We can organize our code in this manner. The document stipulates that a program or application must include a...

4 minutes read.

Java Enum vs Class

Enumerations are used in programming languages to represent collections of named constants. For instance, the four suits in a deck of playing cards might represent four integrators named Club, Diamond,...

7 minutes read.