Skip to content Skip to sidebar Skip to footer

How to Use Bufferedreader in Java to Read File

How to read contents of a File line past line using BufferedReader in Java ?

Read contents of a File line by line using BufferedReader

Following are the steps to read contents of a File line by line using BufferedReader:

Pace i: Load the file into buffer of BufferedReader.

BufferedReader br = new BufferedReader(new FileReader(filename));

BufferedReader provides an efficient way of reading characters, lines and arrays, from a grapheme stream.

Step two: Use java.io.BufferedReader.readLine() method to get a line and iterative calls to the method brings u.s.a. the subsequent lines.

line = br.readLine();

If stream from the specified file contains characters, readLine() returns characters till the encounter of new line or line feed. And if the stream is empty, i.e., no more characters left in the stream, the method returns null, which is an indication that whole contents of file have been read.

Example 1 – Read File Line past Line using BufferedReader

In this example, we accept a text file named samplefile.txt, and we will read contents of this file, line by line, using BufferedReader class.

samplefile.txt

This is commencement line.  This is second line.  This is third line.  Welcome to www.tutorialkart.com.

Example.java

import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException;  /**  * Read contents of a File line by line using BufferedReader  * www.tutorialkart.com  */ public form Instance {       public static void main(String args[]){     	String filename = "samplefile.txt";         BufferedReader br = null;         String line = "";         try {             br = new BufferedReader(new FileReader(filename));             System.out.println("---------Contents of the file---------\n-------------------------------------\n");             while( (line = br.readLine()) != null){                 System.out.println(line);             }         } catch (FileNotFoundException eastward) {         	Arrangement.err.println("Oops! Please check for the presence of file in the path specified.");             e.printStackTrace();         } catch (IOException e) {         	System.err.println("Oops! Unable to read the file.");         	e.printStackTrace();         }     } }

When the higher up program is run, output to the console is as shown in the following.

Output

---------Contents of the file--------- -------------------------------------  This is beginning line. This is second line. This is third line. Welcome to www.tutorialkart.com.

Notation : Provide the path to the file correctly. In this example, the file is placed at the root of Project Folder (of Eclipse Project).

Decision

In this Java Tutorial, we have seen how to use java.io.BufferedReader and its methodjava.io.BufferedReader.readLine() to read the contents of a file line by line.

Test your Java Basics with our Java Quiz

➥ PDF Download - How to read contents of a File line past line using BufferedReader in Java ?

mullinstwereper.blogspot.com

Source: https://www.tutorialkart.com/java/read-contents-file-line-line-using-bufferedreader-java/

Postar um comentário for "How to Use Bufferedreader in Java to Read File"