Java FileReader Sample Code

Opening and the reading the contents of a text file in Java is easily done using both the FileReader and BufferedReader classes.

The FileReader object is intended to read a stream of characters. The FileReader has three constructors. We used the constructor that takes a String object of a file to read.

The BufferedReader object reads text from a character-input stream, which in our sample code here is the contents of FileReader object.


Java FileReader BufferedReader Snippet

The following Java code snippet is the meat of reading a text file in Java. You start with the path to a file, feed that to the FileReader and then feed that to the BufferedReader object.

    //Create a BufferedReader object and feed it FileReader object
	BufferedReader buffReader = new BufferedReader(new FileReader(fileToRead));
		
	while ((eachLine = buffReader.readLine()) != null)
	{
		//Print out each line of file
		System.out.println(eachLine);
	}
	    
	//Close BufferedReader object
	buffReader.close();


Java FileReader Source Code

The sample Java code below is the complete working source code to read and output a file. FOr our sample purposes, it is just reading the release file that comes with the Java JDK package.

package com.jcd.java.tutorials;

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

public class JavaFileReader 
{

	public static void main(String[] args) 
	{
		//Reference a file to read
		String jdkReleaseFile = "C:\\Program Files\\Java\\jdk1.8.0_151\\release";
		
		//Call our read file method
		readTextFile(jdkReleaseFile);

	}
	
	//MEthod to read a text file
	public static void readTextFile(String fileToRead) 
	{
		String eachLine = "";
		
		try
		{
			//Create a BufferedReader object and feed it FileReader object
			BufferedReader buffReader = new BufferedReader(new FileReader(fileToRead));
		
			while ((eachLine = buffReader.readLine()) != null)
			{
				//Print out each line of file
				System.out.println(eachLine);
			}
	    
			//Close BufferedReader object
			buffReader.close();
		}
		catch(Exception ex)
		{
			System.out.println("Exception: "+ex.toString());
		}
	}

}
JAVA_VERSION="1.8.0_151"
OS_NAME="Windows"
OS_VERSION="5.2"
OS_ARCH="amd64"
SOURCE=" .:f87bdb05cdf5 corba:806915813923 deploy:edda04b809ff hotspot:0b0cc47443c4 hotspot/make/closed:53375e6bbd5d hotspot/src/closed:fd21b5a4cafe hotspot/test/closed:fab8ceabb361 install:e6f0f809e076 jaxp:e8563bf8401f jaxws:f413ead0b46a jdk:6e02d1974296 jdk/make/closed:cc5a820da356 jdk/src/closed:baf5e4de7d34 jdk/test/closed:69a93f0f4ffb langtools:3ae1330eb988 nashorn:ea1233a9d4bb pubs:8bfffde64b2e sponsors:7d96dc413ad5"
BUILD_TYPE="commercial"


Hope you found the tutorial on reading a text file in Java and are able to use the working sample Java code in your own Java application or program. Remember to call close() on the BufferedReader object because it’s a good coding practice to cleanup afterwards.



Java References:

Add a Comment

Your email address will not be published. Required fields are marked *