Reading Text File Line by Line in Java 6

Here is code you will need in Java 6 to read a file on the machine. It uses BufferReader through FileReader. Some of the things you should remember are -

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

public class FileLineByLine {

	public static void main(String[] args) {
		FileLineByLine fileLBL = new FileLineByLine();
		fileLBL.readFileLineByLine();

	}

	public void readFileLineByLine(){
		BufferedReader buffReader = null;
		try{
			buffReader = new BufferedReader (new FileReader("C:\\SampleFile.txt"));
			String line = buffReader.readLine();
			while(line != null){
				System.out.println(line);
				line = buffReader.readLine();
			}
		}catch(IOException ioe){
			ioe.printStackTrace();
		}finally{
			try{
				buffReader.close();
			}catch(IOException ioe1){
				//Leave It
			}
		}
	}
}
  • Share/Bookmark
Java

If you enjoyed this post, please consider to leave a comment or subscribe to the feed and get future articles delivered to your feed reader.

Leave Comment

(required)

(required)