Home » Core Java » io » BufferedReader » Read file with BufferedReader example

About Byron Kiourtzoglou

Byron is a master software engineer working in the IT and Telecom domains. He is an applications developer in a wide variety of applications/services. He is currently acting as the team leader and technical architect for a proprietary service creation and integration platform for both the IT and Telecom industries in addition to a in-house big data real-time analytics solution. He is always fascinated by SOA, middleware services and mobile development. Byron is co-founder and Executive Editor at Java Code Geeks.

Read file with BufferedReader example

In this example we are going to see how to use use the BufferedReader class in Java in order to read a simple text file. In Java, there is a number of ways that you can use to read a file, but the BufferedReader class offers one the most efficient and easy to handle tools. Note that the BufferedReader class can be used in order to read any kind of InputStream.

Let’s see the code:
 
 
 
 
 

package com.javacodegeeks.java.core;

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

public class BufferedReaderExample {

	private static String filepath = "C:\\Users\\nikos7\\Desktop\\words.txt";

	public static void main(String[] args) {

		BufferedReader bufferedReader = null;

		try {

			String inputLine;

			bufferedReader = new BufferedReader(new FileReader(filepath));

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

		} catch (IOException e) {

			e.printStackTrace();
		} finally {
			try {

				if (bufferedReader != null) {
				
					bufferedReader.close();
				}

			} catch (IOException ex) {

				ex.printStackTrace();

			}
		}

	}
}

In JDK 1.7 you can also the try-with-resources new super cool feature that automatically closes the stream:

package com.javacodegeeks.java.core;

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

public class BufferedReaderExample {

	private static String filepath = "C:\\Users\\nikos7\\Desktop\\words.txt";

	public static void main(String[] args) {


		try (BufferedReader bufferedReader = new BufferedReader(new FileReader(filepath)) ){

			String inputLine;

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

		} catch (IOException e) {

			e.printStackTrace();
		} 

	}
}

So this will automatically execute the code in the finally clause that we saw above.

The output of the above programs when we read a file with some words:

Output:

anatomy
animation
applet
application
argument
bolts
class
communicate
component
container
development
environment
exception
graphics
image
input
integrate
interface
Java
language
native
network
nuts
object
output
primer
program
security
stream
string
threads
tools
user 

 
This was an example on how to use a BufferedReader to read from a File in Java.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

 

Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments