public class OpenFileDemo
1: import java.io.*;
2: import java.util.*;
3:
4: public class OpenFileDemo
5: {
6: public static void main(String[] args)
7: {
8: try
9: {
10: ObjectOutputStream outputStream = openFile();
11: int n;
12:
13: System.out.println("Enter nonnegative integers.");
14: System.out.println("Place a negative number at the end.");
15: Scanner keyboard = new Scanner(System.in);
16:
17: do
18: {
19: n = keyboard.nextInt();
20: outputStream.writeInt(n);
21: }while (n >= 0);
22:
23: System.out.println("Numbers and sentinel value");
24: System.out.println("written to the file.");
25: outputStream.close();
26: }
27: catch(IOException e)
28: {
29: System.out.println("Problem with output to file.");
30: }
31: }
32:
33:
34: public static ObjectOutputStream openFile( ) throws IOException
35: {
36: ObjectOutputStream tempStreamName;
37: System.out.println("Enter file name:");
38: Scanner keyboard = new Scanner(System.in);
39: String fileName = keyboard.next( );
40: tempStreamName =
41: new ObjectOutputStream(new FileOutputStream(fileName));
42: return tempStreamName;
43: }
44:
45: }