Friday, March 31, 2017

Java BufferedReader with example

In this article we will talk about BufferedReader class in Java with examples. While writing the data by using File Writer,program is responsible to insert line separators manually. We can read the data character by character by using FileReader.It increases the number of I/O operations and effect performance. To overcome this problems sun people has introduced BufferedReader and BufferedWriter classes.

BufferedReader is a wrapper for both "inputStreamReader/FileReader",which buffers the information each time a native I/O is called.BufferedReader improves the performance of the buffering input. It has two constructors. They are

  1. BufferedReader(Reader inputStream)
  2. BufferedReader(Reader inputStream,int bufSize)
The first one creates a buffered character stream using a default buffer size.In the second,the size of the buffer is passed in bufSize.

BufferedReader never communicates directly with the file.It communicates with some reader object only.BufferedReader  class has some important methods they are


  • int read()
  • int read(char[] ch)
  • String readLine();//it is used to reads the next line present in the file.If there is no next line it returns the null
  • void close()
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

In the above statement BufferedReader is a class from Java.io package. This class is used to read text from a character input stream,buffering characters so as to provide for the efficient reading of characters,arrays and lines. 

br is a identifier(variable) to hold the input data.

new is used to instantiate or construct the new class

BufferedReader() method is used to instantiate or construct  an instance of the BufferedReader class.

InputStreamReader() class is come from java.io package. It is used as a bridge from byte streams to character streams. it reads bytes and decodes them into characters.It usually wrapped inside a BufferedReader.

System.in has an object representing input from a buffer that means standard input device on a computer system,usually a keyboard.

Example:

import java.io.*;
class test
{
public static void main(String arg[])throws IOException
{
FileReader fr = new FileReader("myfile.txt");
BufferedReader br = new BufferedReader(fr);
String s = br.readLine();
while(s != null)
{
System.out.println(s);
s = br.readLine();
}
br.close();
}
}

Note: Whenever we are closing BufferedReader,automatically FileReader object will be closed.

Example 2:

The following program shows to read data from any text file

import java.io.*;
class ReadFile
{
public static void main(String[] args)throws IOException
{
//to accept file name from the keyboard

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter file name:");
String fname=br.readLine();
//attach the file to the FileInputStream
FileInputStream fin=null;//assign nothing to fin
//check if file exists or not
try{
fin=new FileInputStream(fname);

}
catch(FileNotFoundException fe)
{

System.out.println("File Not Found");
return;
}
//attach FileInputStream to BufferedInputStream
BufferedInputStream bin=new BufferedInputStream(fin);
System.out.println("file contents:");
//read characters from BufferedInputStream and write them into monitor.Repeat this till the end of file
int ch;
while(ch=bin.read()!=-1)
System.out.println((char)ch);
//close the file
bin.close();
}
}

Output:
Enter file name: myfile.txt
File contents:
hi how are you

Note:  If you entered wrong file name that means does not exist file name in your computer then it display as result File Not Found





No comments:

Post a Comment

High Paying Jobs after Learning Python

Everyone knows Python is one of the most demand Programming Language. It is a computer programming language to build web applications and sc...