---
description: Java BufferedReader is used to read text file in java. Learn Java Bufferedreader with code and syntax example in this tutorial.
title: Java BufferedReader: How to Read File in Java with Example
---

 

[Skip to content](#main) 

## How to read a file in Java?

Java provides several mechanisms to read from File. The most useful package that is provided for this is the java.io.Reader. This class contains the Class Java BufferedReader under package java.io.BufferedReader

## What is BufferedReader in Java?

 **BufferedReader** is a Java class to reads the text from an Input stream (like a file) by buffering characters that seamlessly reads characters, arrays or lines. In general, each read request made of a Reader causes a corresponding read request to be made of the underlying character or byte stream.

It is therefore advisable to wrap BufferedReader in Java around any Reader whose read() operations may be costly, such as java FileReaders and InputStreamReaders. A typical usage would involve passing the file path to the BufferedReader in Java as follows:

objReader = new BufferedReader(new FileReader("D:\DukesDiary.txt"));
//Assuming you have a text file in D drive

This basically loads your file in the objReader.Now, you will need to iterate through the contents of the file and print it.

The [while loop](https://www.guru99.com/how-to-use-loops-in-javascript.html) in the below code will read the file until it has reached the end of file

while ((strCurrentLine = objReader.readLine()) != null) {
    System.out.println(strCurrentLine);
}

strCurrentLine reads the current line and the Java readLine function objReader.readLine() returns a string. Hence, the loop will iterate until it’s not null.

## BufferedReader Example

Below code is a Java BufferedReader example which shows the complete implementation:

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

public class ReadFileExample {

 public static void main(String[] args) {
  BufferedReader objReader = null;
  try {
   String strCurrentLine;

   objReader = new BufferedReader(new FileReader("D:\\DukesDiary.txt"));

   while ((strCurrentLine = objReader.readLine()) != null) {

    System.out.println(strCurrentLine);
   }

  } catch (IOException e) {

   e.printStackTrace();

  } finally {

   try {
    if (objReader != null)
     objReader.close();
   } catch (IOException ex) {
    ex.printStackTrace();
   }
  }
 }
}

Note:

The above code has some very important handlings especially in the finally block of the code.

This code will ensure that the [memory management](https://www.guru99.com/os-memory-management.html) is done efficiently and the objReader.close() method is called that releases the memory.

## BufferedReader JDK7 Example

Below is the example of Java Read Files using BufferedReader class

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

public class ReadFileExample_jdk7 {

 private static final String FILENAME = "D:\\DukesDiary.txt";

 public static void main(String[] args) {

  try (BufferedReader br = new BufferedReader(new FileReader(FILENAME))) {

   String strCurrentLine;

   while ((strCurrentLine = br.readLine()) != null) {
    System.out.println(strCurrentLine);
   }

  } catch (IOException e) {
   e.printStackTrace();
  }
 }
}

#### Summarize this post with:

ChatGPT Perplexity Grok Google AI 

**Stay Updated on AI** **Get Weekly AI Skills, Trends, Actionable Advice.** 

##### Sign up for the newsletter

Subscribe for Free 

You have successfully subscribed.  
Please check your inbox. 

![AI-Newsletter](https://www.guru99.com/images/footer-email-avatar-imges-1.png) Chosen by over **350,000+** professionals 

[Scroll to top ](#wrapper)Scroll to top 

× 

Toggle Menu Close 

Search for: 

Search

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://www.guru99.com/#organization","name":"Guru99","sameAs":["https://www.facebook.com/Guru99Official","https://twitter.com/guru99com"],"logo":{"@type":"ImageObject","@id":"https://www.guru99.com/#logo","url":"https://www.guru99.com/images/guru99-logo-v1-150x59.png","contentUrl":"https://www.guru99.com/images/guru99-logo-v1-150x59.png","caption":"Guru99","inLanguage":"en-US"}},{"@type":"WebSite","@id":"https://www.guru99.com/#website","url":"https://www.guru99.com","name":"Guru99","publisher":{"@id":"https://www.guru99.com/#organization"},"inLanguage":"en-US"},{"@type":"BreadcrumbList","@id":"https://www.guru99.com/buffered-reader-in-java.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":"1","item":{"@id":"https://www.guru99.com","name":"Home"}},{"@type":"ListItem","position":"2","item":{"@id":"https://www.guru99.com/java-tutorials","name":"Java Tutorials"}},{"@type":"ListItem","position":"3","item":{"@id":"https://www.guru99.com/buffered-reader-in-java.html","name":"Java BufferedReader: How to Read File in Java with Example"}}]},{"@type":"WebPage","@id":"https://www.guru99.com/buffered-reader-in-java.html#webpage","url":"https://www.guru99.com/buffered-reader-in-java.html","name":"Java BufferedReader: How to Read File in Java with Example","dateModified":"2023-12-26T15:42:10+05:30","isPartOf":{"@id":"https://www.guru99.com/#website"},"inLanguage":"en-US","breadcrumb":{"@id":"https://www.guru99.com/buffered-reader-in-java.html#breadcrumb"}},{"@type":"Person","@id":"https://www.guru99.com/author/james","name":"James Hartman","description":"I am James Hartman, a seasoned professional in Oracle Certified Java Professional tutorials, specializing in crafting comprehensive guides to help you excel in your Java certification journey.","url":"https://www.guru99.com/author/james","image":{"@type":"ImageObject","@id":"https://www.guru99.com/images/james-hartman-author-v2-120x120.png","url":"https://www.guru99.com/images/james-hartman-author-v2-120x120.png","caption":"James Hartman","inLanguage":"en-US"},"worksFor":{"@id":"https://www.guru99.com/#organization"}},{"@type":"NewsArticle","headline":"Java BufferedReader: How to Read File in Java with Example","keywords":"java","dateModified":"2023-12-26T15:42:10+05:30","articleSection":"Java Tutorials","author":{"@id":"https://www.guru99.com/author/james","name":"James Hartman"},"publisher":{"@id":"https://www.guru99.com/#organization"},"description":"Java BufferedReader is used to read text file in java. Learn Java Bufferedreader with code and syntax example in this tutorial.","copyrightYear":"2023","copyrightHolder":{"@id":"https://www.guru99.com/#organization"},"name":"Java BufferedReader: How to Read File in Java with Example","@id":"https://www.guru99.com/buffered-reader-in-java.html#richSnippet","isPartOf":{"@id":"https://www.guru99.com/buffered-reader-in-java.html#webpage"},"inLanguage":"en-US","mainEntityOfPage":{"@id":"https://www.guru99.com/buffered-reader-in-java.html#webpage"}}]}
```
