×

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

Static and Dynamic Binding in Java

Data Binding in Java The relation between a class and method or class and field is known as Data Binding. In Java, we can create a class for Student_info, but until we...

2 minutes read.

Root exception in java

Java: The main feature of java which is not in C or object oriented programming language is platform independence. Not only the platform independence there are many other features in java...

3 minutes read.

Sort Dates in Java

The Collection class's sort() method, which is a component of the Comparator mechanism, arranges the data in decreasing order. The Comparator interface can be used to accomplish the goal while...

4 minutes read.

Java AWT

Java AWT Java programming is used to develop different types of applications like window-based applications, web applications, Enterprise applications, or mobile applications. For creating standalone applications, Java AWT API is used....

11 minutes read.

Java String trim() method

Java String trim() method returns String after eliminating leading and trailing spaces. Note:- Java String trim() method doesn't trim or omit middle spaces. Syntax: public String trim() Returns: It returns string with omitted leading and...

2 minutes read.

Java FileNotFoundException

FileNotFoundException is another exception class accessible in the java.io bundle. The exemption happens when we attempt to get to that document which isn't accessible in the framework. It is a checked...

5 minutes read.

Decagonal Numbers in Java

This section explains what is a decagonal number and how to write Java programmes that compute decagonal numbers. Both academics and Java programmer interviews regularly question about the Decagonal number...

3 minutes read.

Multiple Inheritance Programs in Java

A component of the object-oriented notion known as multiple inheritances allows a class to inherit properties from multiple parent classes. When methods that have the same signature are present in...

4 minutes read.

Creating API Document Javadoc tool

The JavaDoc utility is a document generator tool written in Java that generates standard documentation in HTML format. It parses declarations and documentation in a source file collection that describes...

3 minutes read.

How to open the Java control panel

The many methods for launching the Java control panel will be covered in this section. We will also go through how the Java Control Panel can be used. Java Control Panel The...

2 minutes read.

How to Reverse a String in Java

How to Reverse a String in Java There are a lot of ways to reverse a string in Java. One can use iteration, StringBuilder, StringBuffer to do the reverse of a...

6 minutes read.

How to make Java Projects

Ant and Maven are both offered by NetBeans for the development of Java applications. When using Ant, the IDE creates an Ant build script depending on the settings you select...

6 minutes read.

Converting Long to Date in Java

What Long and Date are in Java and how are they implemented in the Java programming language are the topics of this article. Additionally, we'll go into great detail on...

4 minutes read.

Difference between throw and throws in java

This article shows you the core difference between “throw” and “throws”keywords in Java programming language.The throw keyword tells Java you want another part of the code to deal withthe exception,...

2 minutes read.

Java Boolean Keyword

Boolean keyword In java programming language we basically have two types of primitive data types, Boolean and Numeric (integer and floating-point data types). In this article we are going to learn...

4 minutes read.

Date time API in java

Introduction: In this text, we can talk approximately Data time API in java. The java.time, java.util, java.sql, and java.text packages contain classes that represent dates and times. The following classes are...

4 minutes read.

New Features of Java 14

 Features like Switch expressions and text blocks which are previewed in Java 13 version are standardized in Java 14. Features in Java 14 Switch ExpressionText BlocksRecordsNull Pointer ExceptionsInstance ofPackaging ToolGarbage collectors 1.Switch Expressions Switch...

3 minutes read.

Java Programming certification

The most common technology utilized in the creation of applications is Java. People and businesses like it because it turns original ideas into useful software solutions. A Java programming certification can...

6 minutes read.

Java Continue Keyword

The java keyword ‘continues’ has also been called as java continue statement.The main concepts of the java continue statement or keyword is used in the controlling of the loop structure.Java...

3 minutes read.

Char and String differences in Java

Characters in Java Character (char) belongs to the characters group, which represents symbols in a character set, such as alphabets and numerals. A Java char has 16 bits in length and has a range...

5 minutes read.