Tuesday, March 29, 2016

Java I/O Examples : How To Read A Text File Line By Line

Intro


This examples will demonstrates how to read a text file line by line. To get this, you can use readLine() method in the BufferedReader class.

Examples


package com.freesamplecode.java.io;

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

public class ReadFileByLineDemo {
 public static void main(String[] args){
  
  String strLine = "";
  String fileName = "D:/temp/test/file_test.txt";
  try {
   
   BufferedReader br = new BufferedReader(new FileReader(fileName));
   while((strLine = br.readLine()) != null){
    System.out.println(strLine);
   }
  } catch (FileNotFoundException e) {
   System.err.print("Unable to find file :"+fileName);
   e.printStackTrace();
  } catch (IOException e) {
   System.err.print("Error encountered when accessing file "+fileName);
   e.printStackTrace();
  }
  
 }
}

Output


Hello,
Welcome to http://free-samplecode.blogspot.com
May this website useful for you.

Thanks,

Admin

Screenshoot


How To Read A Text File Line By Line In Java

Related Posts:

0 comments: