public class BinaryOutputDemo
1:
2: import java.io.*;
3: import java.util.*;
4:
5: public class BinaryOutputDemo
6: {
7: public static void main(String[] args)
8: {
9: try
10: {
11: ObjectOutputStream outputStream =
12: new ObjectOutputStream(new FileOutputStream("numbers.dat"));
13: int n;
14: Scanner keyboard = new Scanner(System.in);
15: System.out.println("Enter nonnegative integers.");
16: System.out.println("Place a negative number at the end.");
17: do
18: {
19: n = keyboard.nextInt( );
20: outputStream.writeInt(n);
21: }while (n >= 0);
22: System.out.println("Numbers and sentinel value");
23: System.out.println("written to the file numbers.dat.");
24: outputStream.close( );
25: }
26: catch(IOException e)
27: {
28: System.out.println("Problem with output to file numbers.dat.");
29: }
30: }
31: }