Java - Console reader() method



Description

The Java Console reader() method retrieve the unique Reader object associated with this console.

Declaration

Following is the declaration for java.io.Console.reader() method −

public Reader reader() 

Parameters

NA

Return Value

This method returns the reader associated with the console.

Exception

NA

Important Note

System.console() may return null in IDEs like Eclipse, NetBeans or IntelliJ. Run these programs from the command line (terminal) for proper execution.

Example - Usage of Console reader() method

The following example shows the usage of Java Console reader() method.

ConsoleDemo.java

package com.tutorialspoint;

import java.io.Console;
import java.util.Scanner;

public class ConsoleDemo {
   public static void main(String[] args) {      
      Console console = null;
      Scanner scan = null;
      
      try {
         // creates a console object
         console = System.console();

         // if console is not null
         if (console != null) {
            
            // prints
            System.out.print("Enter name : ");
            
            // create new scanner object
            scan = new Scanner(console.reader());
            
            // read till the end of data
            while (scan.hasNext()) {
               
               // read next
               String str = scan.next();
               
               // print
               System.out.println(str);
            }
         }      
         
      } catch(Exception ex) {
         // if any error occurs
         ex.printStackTrace();      
      }
   }
}

Console Interaction

Let us compile and run the above program, this will produce the following result −

Enter name : Master Programmer
Master
Programmer

Example - Reading a Single Line from Console

The following example shows the usage of Java Console reader() method. This program uses the reader() method to read user input and display it.

ConsoleDemo.java

package com.tutorialspoint;

import java.io.Console;
import java.io.Reader;
import java.io.BufferedReader;
import java.io.IOException;

public class ConsoleDemo {
   public static void main(String[] args) {
      Console console = System.console(); // Get Console object

      if (console == null) {
         System.out.println("No console available.");
         return;
      }

      Reader reader = console.reader(); // Get Reader object
      BufferedReader bufferedReader = new BufferedReader(reader); // Wrap Reader in BufferedReader

      try {
         System.out.print("Enter a message: ");
         String message = bufferedReader.readLine(); // Read a single line
         console.format("You entered: %s%n", message);
      } catch (IOException e) {
         System.out.println("Error reading input.");
      }
   }
}

Console Interaction

Let us compile and run the above program, this will produce the following result −

Enter a message: Tutorials Point is an excellent web site !
You entered: Tutorials Point is an excellent web site !

Explanation

  • System.console() is used to get a Console object.

  • The reader() method returns a Reader object.

  • BufferedReader is used to read a full line of input.

  • The user enters a message, which is displayed using console.format().

Example - Reading Multiple Lines Until User Stops

The following example shows the usage of Java Console reader() method. This program reads multiple lines using reader() and stops when the user enters "exit".

ConsoleDemo.java

package com.tutorialspoint;

import java.io.Console;
import java.io.Reader;
import java.io.BufferedReader;
import java.io.IOException;

public class ConsoleDemo {
   public static void main(String[] args) {
      Console console = System.console();

      if (console == null) {
         System.out.println("No console available.");
         return;
      }

      Reader reader = console.reader();
      BufferedReader bufferedReader = new BufferedReader(reader);

      try {
         String line;
         console.format("Enter multiple lines (type 'exit' to stop):%n");

         while ((line = bufferedReader.readLine()) != null) {
            if (line.equalsIgnoreCase("exit")) {
               break;
            }
            console.format("You entered: %s%n", line);
         }
      } catch (IOException e) {
         System.out.println("Error reading input.");
      }
   }
}

Console Interaction

Let us compile and run the above program, this will produce the following result −

Enter multiple lines (type 'exit' to stop):
Java
You entered: Java
C++
You entered: C++
Python
You entered: Python
exit

Explanation

  • reader() is used to get a Reader object from Console.

  • BufferedReader.readLine() reads multiple lines.

  • The loop continues reading until the user enters "exit", then it stops.

java_io_console.htm
Advertisements