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
0 comments: