Program to read text using Bufferedreader in Java

Advertisements
Advertisements

 8 total views

Java programming language has many classes to read input characters. One such class is BufferedReader class and this program to read text using the BufferedReader in Java demonstrates the working of this class.

We are using JDK 8u111 with Netbeans 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 uses 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 in Java and display the characters in integer equivalent.

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

Advertisements

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

Related Articles:-


Advertisements
Advertisements
Advertisements