Please disable your adblock and script blockers to view this page

Search this blog

Showing posts with label BufferedReader. Show all posts
Showing posts with label BufferedReader. Show all posts

Thursday 22 November 2018

Read Text File in Java using BufferedReader

 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

  1. package client;
  2. import java.io.BufferedReader;
  3. import java.io.File;
  4. import java.io.FileNotFoundException;
  5. import java.io.FileReader;
  6. import java.io.IOException;
  7. public class ReadFileBfr {
  8. public ReadFileBfr() {
  9. super();
  10. }
  11. public static void main(String[] args) {
  12. // Absolute path of file that you want to read
  13. File f = new File("D:/sampleFile.txt");
  14. BufferedReader bfr=null;
  15. try {
  16. bfr = new BufferedReader(new FileReader(f));
  17. } catch (FileNotFoundException e) {
  18. }
  19. String data;
  20. //Iterate over file content
  21. try {
  22. while ((data = bfr.readLine()) != null)
  23. System.out.println(data);
  24. } catch (IOException e) {
  25. }
  26. }
  27. }

Execute this java code

Output is


Cheers ðŸ™‚ Happy Learning