Please disable your adblock and script blockers to view this page

Search this blog

Showing posts with label FileReader. Show all posts
Showing posts with label FileReader. Show all posts

Saturday 24 November 2018

Read Text File in Java using FileReader Class

 Previously I have posted about reading text file using FileInputStreamBufferedReader and Scanner Class. This post shows how to read text files in java using FileReader class.


This is a sample text file


and this is the java code to read text files using FileReader class

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

Execute this java code

Output is


Cheers 🙂 Happy Learning