Saturday, April 27, 2013

How to read a file using bufferedreader in java

Category: , ,


Java is an object oriented computer programming language. It was originally designed by James Gosling and Sun Microsystems and merged into Oracle Corporation later. Since then it was developed and in 2011, the latest version JDK 7.0 was released.

File handling is an important aspect in Java as well as many other programming languages for learners. There are a handful of ways to read a file. But in this article you will be guided how to use a buffered reader to read from a file in Java.

Buffered reader method is a simple and commonly used method to read from a file. Let’s see how to implement a BufferedReader to read from a file.

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

/**
 *
 * @author Mystery Guest
 * 
 */

class BufferedReaderGuide {
    public static void main(String[] args) {
        
        BufferedReader br = null;
        String fileName = "Text File.txt";
        String lineFromFile;
        
        try {
            FileReader fr = new FileReader(fileName);
            br = new BufferedReader(fr);
            
            while ((lineFromFile = br.readLine()) != null) {                
                System.out.println(lineFromFile);
            }
        } catch (IOException e) {
            System.out.println(e.getMessage());
        } finally {
            if(br != null) {
                try {
                    br.close();
                } catch (IOException ex) {
                    System.out.println(ex.getMessage());
                }
            }
        }
    }
}


Explanation
BufferedReader Class :
BufferedReader is a class contained in java.io package. It is inherited from the class reader which is again inherited from the root class of the java class hierarchy. Buffered reader can be used to read characters, arrays and lines from a character input stream efficiently with buffering.

Exceptions :
There can be occurred a FileNotFoundException in declaration of FileReader or BufferedReader. It should be caught or declared to be thrown in method declaration. The outer try catch block provides that exception handling.

FileReader Object :
In this example we have selected the FileReader object with a constructor of String parameter which takes the file path as the String. There are other too to initialize a FileReader.

BufferedReader Object :
BufferedReader constructor takes a Reader object as a parameter. Hence, the FileReader object is passed.

Reading a text line :
readLine() method in BufferedReader class is used to reader characters from a file till it meets an endline character. There are so many other methods which can be used in BufferedReader class. read() and skip() are 2 such methods which are most commonly used in reading from a file.

Closing the BufferedReader stream :
The finally block is used to close the BufferedReader stream and release all the stream resources used.

Enhancements :
However in JDK 7.0, using the new feature “try-with-resources” we can close the file automatically and it will be shown in the next article.


Subscribe to our newsletter from here

0 comments:

Post a Comment