• Reading a text file line by line in java can be done by using java.io.BufferedReader .
  • Create a BufferedReader class by passing new FileReader(new File("filename")) object to it's constructor.
  • By using readLine() method of  BufferedReader class we can read line by line text from a text file as Strings.
  • Lets see a java example program on hoe to read data from file line by line using BufferedReader class readLine() method.
  • Java read lines from text file example program.


#1: Java Example program to read file line by line

 

  1. package com.instanceofjava.readfile;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.File;
  5. import java.io.FileReader;
  6. import java.io.IOException;
  7. /** *  * 
  8. @author www.instanceofjava.com 
  9. * @category: Interview Programs 
  10. * @description: How to read text file line by line in java example program 
  11. * */
  12.  
  13. public class ReadFile {
  14.  
  15.     public static void main(String[] args) {        
  16.         
  17.         try {            
  18.            File fileName = new File("E:\\data.txt");
  19.             FileReader fileReader = new FileReader(fileName);     
  20.             BufferedReader bufferedreader = new BufferedReader(fileReader);           
  21.             StringBuffer sb = new StringBuffer();
  22.             String strLine;            
  23.            while ((strLine = bufferedreader.readLine()) != null) {
  24.                 sb.append(strLine);
  25.                 sb.append("\n");        
  26.             }         
  27.            fileReader.close();
  28.            System.out.println(sb.toString()); 
  29.          } catch (IOException e) {
  30.             e.printStackTrace();     
  31.    }
  32.  
  33.     }
  34.  
  35. }

Output:

  1. java read file line by line example
  2. java read file line by line java 8
  3. java read lines from text file 
  4. example bufferedreader java example

#2: Java example program to read text file line by line using java 7 try with resource example



read file line by line java


#3: Java example program to read text file line by line using java 8 example (Using stream)

  1. package com.instanceofjava.readfile;
  2. import java.io.IOException;
  3. import java.nio.file.Files;
  4. import java.nio.file.Paths;
  5. import java.util.stream.Stream;
  6. /**
  7.  *
  8. * @author www.instanceofjava.com
  9.  * @category: Interview Programs 
  10. * @description: How to read text file line by line in java example program using java 8 stream
  11. */
  12. public class ReadFile {
  13. public static void main(String[] args) throws IOException {
  14.  
  15. try (Stream<String> stream = Files.lines(Paths.get("E:\\data.txt"))) 
  16. {           
  17.  
  18.  stream.forEach(System.out::println);
  19.  
  20. }
  21. }
  22. }

Instance Of Java

We will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
«
Next
Newer Post
»
Previous
Older Post

No comments

Leave a Reply

Select Menu