Java Program to Read a File and Display its Contents

This post covers a program in Java that reads a file and display its contents.

Read and Display the Content of a File in Java

The question is, write a Java program to read and display the content of a given file. The name of file must be received by user at run-time of the program. The program given below is the answer to this question:

import java.util.Scanner;
import java.io.*;

public class fresherearth
{
    public static void main(String[] args)
    {
        String fname;
        Scanner scan = new Scanner(System.in);
        
        // enter filename along with its extension
        System.out.print("Enter the Name of File: ");
        fname = scan.nextLine();
        
        String line = null;
        try
        {
            FileReader fileReader = new FileReader(fname);
            
            // always wrap the FileReader in BufferedReader
            BufferedReader bufferedReader = new BufferedReader(fileReader);
            
            while((line = bufferedReader.readLine()) != null)
            {
                System.out.println(line);
            }
            
            // always close the file after its use
            bufferedReader.close();
        }
        catch(IOException ex)
        {
            System.out.println("\nError occurred");
            System.out.println("Exception Name: " +ex);
        }
    }
}

The snapshot given below shows the sample run of above program, with user input fresherearth.txt as the name of file to read and prints its content on the output screen:

Java Program read display file content

Since, I've already created the file named fresherearth.txt inside the current directory. Therefore, the above program displays the content of that file. Now let me try with a name of file, that does not exist inside the current directory:

java read a file and display its contents

That is, the exception thrown is, java.io.FileNotFoundException: none.txt (The system cannot find the file specified), saying that the entered file named none.txt is not available inside the current directory.

The current directory means the directory, where the above source code (above Java program) is saved. Here is the snapshot of the current directory in my case, along with opened file fresherearth.txt:

java read and display the content of file

The above program can also be created in this way:

import java.util.Scanner;
import java.io.*;

public class fresherearth
{
    public static void main(String[] args)
    {
        Scanner scan = new Scanner(System.in);
        
        System.out.print("Enter the Name of File: ");
        String fileName = scan.nextLine();
        
        String myline = null;
        try
        {
            FileReader fr = new FileReader(fileName);
            BufferedReader br = new BufferedReader(fr);
            
            System.out.println("\n----The content of File----");
            myline = br.readLine();
            while(myline != null)
            {
                System.out.println(myline);
                myline = br.readLine();
            }
            br.close();
        }
        catch(IOException ex)
        {
            System.out.println("\nError occurred");
            System.out.println("Exception Name: " +ex);
        }
    }
}

Here is its sample run with same user input as of first sample run:

java read and display file

Same Program in Other Languages

Java Online Test


« Previous Program Next Program »