On this tutorial we will be showing different ways on how to do java file reading line by line using FileReader, BufferedReader and Scanner. We have provided each scenario a java example which you can readily copied and compiled for your own use. Depending on your business requirements, you can select your own implementation.

File class provides functionality to read and write on File. It takes an argument string filename, depending your platform it takes different format. Like in unix the prefix of an absolute filename is always / while in windows the prefix of the pathname that contains a drive specifier consists of the drive letter followed by “:” and possibly followed by “\\”.

Depending on your situation and requirements in reading files, you can select on any below java examples as appropriate.

Java file reading line by line in java using BufferedReader and FileReader

package com.javatutorialhq.tutorial;

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

public class FileReadBuffer {

	/**
	 * This java sample code shows how to read file
	 * using BufferedReader and FileReader
	 * FileReader example
	 * BufferedReader example
	 * Property of javatutorialhq.com
	 * All Rights Reserved
	 * Version 1.0
	 * 04/21/2012
	 */
	public static void main(String[] args) {
		File fileInput = new File("C:\\temp\\1.txt");
		try {
			//Check if file Exists
			if(fileInput.exists() && fileInput.canRead()){
				BufferedReader buffer = new BufferedReader(new FileReader(fileInput));
				String charHolder;
				while((charHolder=buffer.readLine())!= null){
					System.out.println(charHolder);
				}
				buffer.close();
			}
			else
				System.out.println("File:"+fileInput+" does not exist");

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

	}

}

The code above read the contents of file with filename C:\temp\1.txt and then it prints the contents line through java console. Object buffer is of type BufferedReader. The java BufferedReader class takes into its constructor a Reader class. We used a FileReader class as a Reader input parameter to BufferedReader. Constructor of Reader class takes an object of File type. This would come in our first object which is fileInput of type File. BuffreredReader class exposes a method readLine which satisfies our requirement to read file in java and gives a line by line output.

Java file reading in java using Scanner

package com.javatutorialhq.tutorial;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class FileReadScanner {

	/**
	 * This java sample code shows how to read file
	 * using Scanner
	 * Property of javatutorialhq.com
	 * All Rights Reserved
	 * Version 1.0
	 * 04/21/2012
	 */
	public static void main(String[] args) {
		try {
			Scanner scan = new Scanner(new File("C:\\temp\\1.txt"));
			while(scan.hasNext()){
				System.out.println(scan.nextLine());
			}
			scan.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}

	}

}

This example shows how to read file line by line in java using Scanner class. The constructor class takes an object File as an argument. Scanner breaks its input into tokens using a delimiter pattern, which by default matches whitespace. The resulting tokens may then be converted into values of different types using the various next methods. In this java example as specified, we are only interested in reading a file and display its output line by line. We will be using a lot of this scanner class on the succeeding tutorials.