4-19 FileReadDemo

posted Mar 5, 2014, 1:45 AM by Dlovan ismail   [ updated Mar 5, 2014, 1:45 AM by Karwan Jacksi ]
//This program reads data from a file.

package filereaddemo;
import java.util.Scanner;  // Needed for the Scanner class
import java.io.*;                   // Needed for the File and lOException

public class FileReadDemo 
{
    public static void main(String[] args)throws IOException
    {
            // Create a Scanner object for keyboard input.
            Scanner keyboard = new Scanner(System.in);

            // Gat the filename.
            System.out.print("Enter the filename: ");
            String filename = keyboard.nextLine();

            // Open the file.
            File file = new File(filename);
            Scanner inputFile = new Scanner(file);

            // Read lines from the file until no more are left.
            while (inputFile.hasNext())
            {
                // Read the next name.
                String friendName = inputFile.nextLine();

                // Display the last name read.
                System.out.println(friendName);
            }

            // Close the file.
            inputFile.close();
        
    }   
}

ċ
FileReadDemo.java
(1k)
Karwan Jacksi,
Mar 5, 2014, 1:45 AM
Comments