HowToDoInJava

  • Java 8
  • Regex
  • Concurrency
  • Best Practices
  • Spring Boot
  • JUnit5
  • Interview Questions
  • Dark Mode

Java BufferedReader Example

By Lokesh Gupta | Filed Under: Java I/O

If you want to read a file in java using BufferedReader, use below code as template and reuse it the way you like.

BufferedReader reads text from a character-input stream, buffering characters so as to provide for the efficient reading of characters, arrays, and lines.

1) Use BufferedReader without try-with-resources (Before Java 7)

package com.howtodoinjava.examples.io;

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

public class BufferedReaderExample 
{
	public static void main(String[] args)
	{
		BufferedReader bufferedReader = null;

		try 
		{

			String currLine;

			bufferedReader = new BufferedReader(new FileReader("C:\\temp\\testOut.txt"));

			while ((currLine = bufferedReader.readLine()) != null)
			{
				System.out.println(currLine);
			}

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

2) Use BufferedReader with try-with-resources (Java 7 Onwards)

package com.howtodoinjava.examples.io;

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

public class BufferedReaderExample 
{
	public static void main(String[] args)
	{
		try (BufferedReader bufferedReader = new BufferedReader(new FileReader("C:\\temp\\testOut.txt")))
		{
 
			String currLine;
 
			while ((currLine = bufferedReader.readLine()) != null) 
			{
				System.out.println(currLine);
			}
 
		} 
		catch (IOException e) 
		{
			e.printStackTrace();
		}
	}
}


Happy Learning !!

TwitterFacebookLinkedinRedditPocket

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Leave a Reply

This comment form is under antispam protection
This comment form is under antispam protection
  Subscribe  
Notify of

Search Tutorials

Java IO Tutorials

  • IO – Introduction
  • IO – How it works?
  • IO – IO vs. NIO
  • IO – Copy Directory Recursively
  • IO – Delete Directory Recursively
  • IO – Create File
  • IO – Write to File
  • IO – Append to File
  • IO – Create Read Only File
  • IO – Read File to String
  • IO – Read File to Byte[]
  • IO – Read File Line by Line
  • IO – BufferedReader
  • IO – BufferedWriter
  • IO – Read/Write Properties File
  • IO – Read file from resources
  • IO – Read/Write UTF-8 Data
  • IO – Check if File Exist
  • IO – File Copy
  • IO – FilenameFilter
  • IO – FileFilter
  • IO – Create Temporary File
  • IO – Write Temporary File
  • IO – Delete Temporary File
  • IO – Read from Console
  • IO – Typesafe input using Scanner
  • IO – String to InputStream
  • IO – InputStream to String
  • IO – Password Protected Zip
  • IO – Unzip with Sub-directories
  • IO – Manage System Log Size
  • IO – Generate SHA / MD5

Popular Tutorials

  • Java 8 Tutorial
  • Core Java Tutorial
  • Collections in Java
  • Java Concurrency
  • Spring Boot Tutorial
  • Spring AOP Tutorial
  • Spring MVC Tutorial
  • Spring Security Tutorial
  • Hibernate Tutorial
  • Python Tutorial
  • Jersey Tutorial
  • Maven Tutorial
  • Log4j Tutorial
  • Regex Tutorial

Meta Links

  • Advertise
  • Contact Us
  • Privacy policy
  • About Me

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2016 · HowToDoInjava.com · All Rights Reserved. | Sitemap

wpDiscuz