public class Question21
1: import java.io.EOFException;
2: import java.io.FileInputStream;
3: import java.io.FileNotFoundException;
4: import java.io.ObjectInputStream;
5: import java.io.IOException;
6: import java.util.Scanner;
8: public class Question21
9: {
10: public static void main(String[] args)
11: {
12: //To use fileName in a catch block,
13: //declare it outside of the try block
14: String fileName = null;
15: try
16: {
17: System.out.println("Enter file name:");
18: Scanner keyboard = new Scanner(System.in);
19: fileName = keyboard.next( );
20: ObjectInputStream inputStream =
21: new ObjectInputStream(
22: new FileInputStream(fileName));
23: System.out.println("The first thing in the file");
24: System.out.println(fileName + " is");
25: String first = inputStream.readUTF( );
26: System.out.println(first);
27: inputStream.close( );
28: }
29: catch(FileNotFoundException e)
30: {
31: System.out.println("Problem opening the file " +
32: fileName);
33: }
34: catch(EOFException e)
35: {
36: System.out.println("Unexpected end of file.");
37: }
38: catch(IOException e)
39: {
40: System.out.println("Problem with input from file " +
41: fileName);
42: }
43: }
44: }