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: System.out.println("The file " + fileName +
16: " contains the following lines:");
17: try
18: {
19: inputStream = new Scanner(new File(fileName));
20: }
21: catch(FileNotFoundException e)
22: {
23: System.out.println("Error opening the file " + fileName + ".");
24: System.exit(0);
25: }
26: while (inputStream.hasNextLine())
27: {
28: String line = inputStream.nextLine();
29: System.out.println(line);
30: }
31: inputStream.close();
32: }
33: }
34: