Java Read from Console Input

In Java 1.0, the only way to perform the console input was to use a byte stream. Today, using a byte stream to read console input is still acceptable. However, for the commercial applications, the preferred method of reading console input is to use a character-oriented stream. This makes your program easier to internationalize and maintain.

In Java, console input is accomplished by reading from System.in. To obtain a character-based stream that is attached to the console, wrap System.in in a BufferedReader object.

BufferedReader supports a buffered input stream. A commonly used constructor is shown below :

BufferedReader(Reader inputReader)

Here, inputReader is the stream that is linked to the instance of BufferedReader that is being created. Reader is an abstract class. One of its concrete subclasses is InputStreamReader, which converts bytes to characters. To obtain an InputStreamReader object that is linked to System.in, use the following constructor:

InputStreamReader(InputStream inputStream)

Because System.in refers to an object of type InputStream, it can be used for inputStream. Putting it all together, the following line of code creates a BufferedReader that is connected to the keyboard:

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

After this statement executes, br is a character-based stream that is linked to console through System.in.

Read Characters in Java

Use read() method, to read a character from a BufferedReader.

The version of read() that we will be using is

int read() throws IOException

Each time that the read() is called, it reads a character from the input stream and returns it as an integer value. It returns -1 when the end of the stream is encountered. As you can see, it can throw an IOException.

The following program demonstrates the read() by reading the characters from the console until the user types a "q". Notice that any I/O exceptions that might be generated are simply thrown out of the main(). Such an approach is common when reading from console in simple example programs, but in more sophisticated applications, you can handle the exceptions explicitly.

/* Java Program Example - Read Console Input in Java
 * Use a BufferedReader to read characters from the console */
 
 import java.io.*;
 
 class ReadConsoleInput
 {
     public static void main(String args[]) throws IOException
     {
         
         char chr;
         
         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
         
         System.out.print("Enter a character (Press 'q' to quit) : ");
         // read characters now
         do
         {
             chr = (char) br.read();
             System.out.println(chr);
             
         } while(chr != 'q');
         
     }
  }

Below is a sample run of the above program :

java read console input

This output may look a little different from what you expected because System.in is line buffered, by default. This means that no input is actually passed to the program until you press ENTER. As you can guess, this doesn't make read() particularly valuable for the interactive console input.

Read Strings in Java

To read a string from the keyboard, use the version of the readLine() method that is a member of the BufferedReader class. Its general form is shown below:

String readLine() throws IOException

As you can see, it returns a String object.

The following program demonstrates the BufferedReader and the readLine() method, the program reads and displays the lines of text until you enter the word "stop" :

/* Java Program Example - Read Console Input in Java
 * Use a BufferedReader to read string from console */
 
 import java.io.*;
 
 class ReadConsoleInput
 {
     public static void main(String args[]) throws IOException
     {
         
         /* create a BufferedReader using System.in */
         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
         
         String str;
         
         System.out.println("Enter some lines of text (enter 'stop' to quit) : ");
         // read string now
         do
         {
             str = br.readLine();
             System.out.println(str);
             
         } while(!str.equals("stop"));
         
     }
  }

Now the next program creates a tiny text editor. It creates an array of String objects and then reads in lines of text, storing each line in the array. It will read up to 80 lines or until you enter "stop". It uses a BufferedReader to read from the console.

/* Java Program Example - Read Console Input in Java
 * Its a small text editor */
 
 import java.io.*;
 
 class ReadConsoleInput
 {
     public static void main(String args[]) throws IOException
     {
         
         /* create a BufferedReader using System.in */
         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
         
         String str[] = new String[80];
         
         System.out.println("Enter some lines of text (Enter 'stop' to quit) : ");
         for(int i=0; i<80; i++)
         {
             str[i] = br.readLine();
             if(str[i].equals("stop"))
             {
                 break;
             }
         }
         
         System.out.println("\nHere is you file :");
         
         /* now display the lines */
         for(int i=0; i<80; i++)
         {
             if(str[i].equals("stop"))
             {
                 break;
             }
             System.out.println(str[i]);
         }
         
     }
 }

Below is sample run of the above Java program :

read console input in java

Examples on Files in Java

Here are some examples related to files in Java, you can go for.

Java Online Test


« Previous Tutorial Next Tutorial »