public class TextFileInputDemo2
1: //TextfileInputDemo2.java
2:
3: import java.io.File;
4: import java.io.FileNotFoundException;
5: import java.util.Scanner;
6:
7: public class TextFileInputDemo2
8: {
9: public static void main(String[] args)
10: {
11: System.out.print("Enter file name: ");
12: Scanner keyboard = new Scanner(System.in);
13: String fileName = keyboard.next();
14: Scanner inputStream = null;
15: try
16: {
17: inputStream = new Scanner(new File(fileName));
18: }
19: catch(FileNotFoundException e)
20: {
21: System.out.println("Error opening the file " + fileName + ".");
22: System.exit(0);
23: }
24: System.out.println("The file " + fileName +
25: " contains the following lines:");
26: while (inputStream.hasNextLine())
27: {
28: String line = inputStream.nextLine();
29: System.out.println(line);
30: }
31: inputStream.close();
32: }
33: }
34: