Previously I have posted about reading text file using FileInputStream and this post shows how to read text files in java using BufferedReader class.
This is a sample text file
and this is the java code to read a text file using BufferedReader class
- package client;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.io.IOException;
- public class ReadFileBfr {
- public ReadFileBfr() {
- super();
- }
- public static void main(String[] args) {
- // Absolute path of file that you want to read
- File f = new File("D:/sampleFile.txt");
- BufferedReader bfr=null;
- try {
- bfr = new BufferedReader(new FileReader(f));
- } catch (FileNotFoundException e) {
- }
- String data;
- //Iterate over file content
- try {
- while ((data = bfr.readLine()) != null)
- System.out.println(data);
- } catch (IOException e) {
- }
- }
- }
Execute this java code
Output is
Cheers Happy Learning