Previously I have posted about reading text file using FileInputStream, BufferedReader and Scanner Class. This post shows how to read text files in java using FileReader class.
- package client;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.io.IOException;
- public class ReadFileFr {
- public ReadFileFr() {
- super();
- }
- public static void main(String[] args) {
- // Absolute path of file that you want to read
- FileReader fr=null;
- try {
- fr = new FileReader("D:/sampleFile.txt");
- } catch (FileNotFoundException e) {
- System.out.println(e);
- }
- int data;
- //Iterate over file content
- try {
- while ((data = fr.read()) != -1)
- System.out.print((char) data);
- } catch (IOException e) {
- System.out.println(e);
- }
- }
- }