java Task 4 Now read the file that you just created This is
java
Task #4– Now read the file that you just created
This is a simple program that simply reads the disk-based file, using the Scanner Class, that you created in task #3.
The user input is the name of the file that you wish to read.
For your output:
Display the contents of the named file that has the heading “The Alphabet”
Display a “File read completed “message to the user
Pseudocode:
Prompt the user for a file name
Append the file extension to the filename
Open the file
Iterate through each line of the file and display the resultant line on the console
Close the file
Display a “Completed reading the file” message to the user
Solution
import java.io.File;
 import java.util.Scanner;
/**
 * program to read text file from file system
 *
 * @author
 *
 */
 public class ReadFileContent {
   /**
    * @param args
    */
    public static void main(String[] args) {
       Scanner scanner = null;
        try {
           scanner = new Scanner(System.in);
            System.out.print(\"Enter the file name:\");
            String fileName = scanner.next();
            // infile.txt
            scanner = new Scanner(new File(fileName));
            while (scanner.hasNext()) {
               System.out.println(scanner.nextLine());
            }
       } catch (Exception e) {
            // TODO: handle exception
        } finally {
           if (scanner != null)
                scanner.close();
        }
    }
 }
infile.txt
The Alphabet
OUTPUT:
Enter the file name:infile.txt
 The Alphabet


