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