Created
March 30, 2015 19:41
-
-
Save khatchad/6241833a58f4d904b752 to your computer and use it in GitHub Desktop.
This program reads data from a file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; // Needed for the Scanner class | |
import java.io.*; // Needed for the File class | |
/** | |
This program reads data from a file. | |
*/ | |
public class FileReadDemo | |
{ | |
public static void main(String[] args) throws IOException | |
{ | |
// Create a Scanner object for keyboard input. | |
Scanner keyboard = new Scanner(System.in); | |
// Get 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(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment