How To Read Data From A Text File In Java Using FileReader Class

How To Read Data From A Text File In Java Using Filereader Class.

VIDEO TUTORIAL

SOURCE CODE

/*
 * How To Read Data From A Text File In Java Using FileReader Class.
 * https://mauricemuteti.info/how-to-read-data-from-a-text-file-in-java-using-filereader-class/
 */
package readfromatextfileusingfilereaderclass;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

/**
 *
 * @author Authentic
 */
public class ReadFromATextfileUsingFileReaderClass {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here

        //Creating file name string
        String fileName = "data.txt";
        //Invoking readFromATextFileUsingFileReaderClassMethod
        readFromATextFileUsingFileReaderClass(fileName);
    }

    /**
     * readFromATextFileUsingFileReaderClass Method.
     *
     * Static method for reading text file data and printing it on the console
     * using file reader class.
     *
     * @param pathName
     */
    public static void readFromATextFileUsingFileReaderClass(String pathName) {
        //Declaring and initializing File Object and passing filename in its constructor.
        File file = new File(pathName);
        //Declaring file reader class..
        FileReader fileReader = null;
        int line;
        //Counter Integer variable
        int lineCounter = 0;
        try {
            System.out.println("Inside try catch block");
            //Inside try catch block.
            fileReader = new FileReader(file);

            //Reading lines from a text file.
            System.out.println("Reading lines from a text file.");
            //While loop - for getting all text file lines.
            while ((line = fileReader.read()) != -1) {
                //Incrementing counter.
                lineCounter++;
                //Print output on the console.
                System.out.print((char) line);
            }
            //Print empty line on the console. 
            System.out.println("");
            System.out.println("Finished Reading lines from a text file.");

        } catch (FileNotFoundException ex) {
            System.out.println("Error : " + ex.getMessage());
        } catch (IOException ex) {
            System.out.println("Error : " + ex.getMessage());
        } finally {
            //Inside finally block.
            //Releasing resources.
            if (fileReader != null) {
                try {
                    System.out.println("Closing file reader.");
                    //Closing file reader.
                    fileReader.close();
                } catch (IOException ex) {
                    System.out.println("Error : " + ex.getMessage());
                }
            }

        }

    }

}

NETBEANS OUTPUT

run:
Inside try catch block
Reading lines from a text file.
Line 1
Line 2
Line 3
Line 4
Line 5
Line 6
Line 7
Line 8
Line 9
Line 10
Line 11
Finished Reading lines from a text file.
Closing file reader.
BUILD SUCCESSFUL (total time: 0 seconds)

TEXT FILE DATA

Line 1
Line 2
Line 3
Line 4
Line 5
Line 6
Line 7
Line 8
Line 9
Line 10
Line 11

SCREENSHOTS

How To Read A Text File In Java Filereader Class
How To Read A Text File In Java Filereader Class
How To Read A Text File In Java Filereader Class

Leave a Reply

Your email address will not be published. Required fields are marked *