Program to read text using Bufferedreader in Java

Java programming language has many classes to read input characters. One such class is BufferedReader class and this program demonstrate the BufferedReader class in Java.

We are using JDK 8u111 with Netbean IDE 8.2 installed on a Windows 7 64-bit PC to compile and run this program. You can also install the JDK and run this program from a windows command console.

Problem Definition

This program use the BufferedReader class to create a new BufferedReader object. The main program is BRRead and the Java program is saved with the same name – BRRead.java. To write a program to read character using Bufferedreader class in Java and display the characters in integer equivalent.

For example, if character isĀ  ‘a’, then output is 97 – ANSI equivalent.

The BufferedReader object uses InputStreamReader another object from InputStreamReader Class that reads a stream of characters.

Program Code

  
import java.io.*;

class BRRead
{
public static void main(String args[])

throws IOException
{
char c;
BufferedReader br = new BufferedReader (new InputStreamReader(System.in));

System.out.println("Enter character,'10' to quit");

// read characters

do {

c= br.read();
System.out.println(c);
}while(c != 10);

}

}

 

Output – BufferedReader Demo

The output of the above program is given below. The program reads a stream of character through BufferedReader object and display the characters when the last read character is 10.

 

  
Enter character,'10' to quit
New York
78
101
119
32
89
111
114
107
10

 

Advertisements