Java.io - SPLessons

Java.io BufferedReader

Home > Lesson > Chapter 4
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Java.io BufferedReader

Java.io BufferedReader

shape Introduction

The Java.io BufferedReader is the class used for reading the characters into a directory or file. The Java.io BufferedReader performance is little bit faster compared to other readers like reading the files into CD drive and in many more applications.

shape Description

The Java.io BufferedReader is similar to that buffered input stream with tiny difference that it reads stream of bytes like data related to images, where as the buffered reader reads the characters like in the text form. The main feature of this class is to read block of characters or data at a time instead of reading data character by character. In buffered reader class, contains one method called read line() method used to read the textual line at a time.

shape Conceptual figure

Class Declaration

shape Declaration

Java.io/BufferedReader class is declared below: Public class BufferedReader extends Reader

Class Constructors

shape Table

Constructor Description
BufferedReader (Reader in) The function of this constructor is to create the Java.io BufferedReader object for input character stream with default buffer size.
BufferedReader (Reader in, int n) The function of this constructor is to create the buffered reader object for input character stream with represented buffer size.

Class Methods

shape Table

Method Description
void close() The function of this method is to close the present stream and associated to input stream systems resources are released.
String readLine () The function of this method is to read the a line of characters or text.
Long skip (long m) The function of this method is to skip the text or characters in this stream of ' m ' bytes.
Boolean ready() The function of this method is to check whether the current stream is ready to read the characters or not.

Inherited Methods

shape Description

From the following classes, methods are inherited the buffered reader class.
  • Java.io.object
  • Java.io.Reader

shape Examples

Usage of void close() method. [c]import java.io.BufferedReader; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; public class BufferedReader { public static void main(String[] args) throws Exception { InputStream is = null; InputStreamReader isr = null; BufferedReader br = null; try{ // To open input stream is = new FileInputStream("c:/test.txt"); // creating new input stream reader isr = new InputStreamReader(is); // creating new buffered reader br = new BufferedReader(isr); // releases all the system resources associated with this reader br.close(); // creates error br.read(); }catch(IOException e) { // IO error exception System.out.println("The buffered reader is closed"); }finally { // releases all the system resources associated with it if(is!=null) is.close(); if(isr!=null) isr.close(); if(br!=null) br.close(); } } }[/c] Output Following is the result will be displayed. [c]The buffered reader is closed.[/c] Usage of Long skip (long m) method. [c]import java.io.BufferedReader; import java.io.StringReader; public class BufferedReader { public static void main(String[] args) throws Exception { String s ="ABCDEFG"; StringReader sr = null; BufferedReader br = null; try{ // creating to assign a new string reader sr = new StringReader(s); // creating new buffered reader br = new BufferedReader(sr); // It used to reads and print BufferedReader int value = 0; while((value = br.read()) != -1) { // To skip a character br.skip(1); System.out.print((char)value); } } catch (Exception e) { // If any exception occurs e.printStackTrace(); }finally { // releases all the system resources associated with this stream if(sr!=null) sr.close(); if(br!=null) br.close(); } } }[/c] Output Following is the result will be displayed. [c]ACEG[/c]

Summary

shape Key Points

  • The buffer reader is the class used for reading the characters into a directory or file.
  • The buffered reader performance is little bit faster compared to other readers like reading the files into CD drive and in many more applications.
  • The main feature of this class is to read block of characters or data at a time instead of reading data character by character.