Java Reference
In-Depth Information
LISTING 15.3
Continued
39: buff.close();
40: return true;
41: } catch (IOException e) {
42: System.out.println(“Exception: “ + e.getMessage());
43: return false;
44: }
45: }
46:
47: boolean readStream() {
48: try {
49: FileInputStream file = new
50: FileInputStream(“numbers.dat”);
51: BufferedInputStream buff = new
52: BufferedInputStream(file);
53: int in = 0;
54: do {
55: in = buff.read();
56: if (in != -1)
57: System.out.print(“ “ + in);
58: } while (in != -1);
59: buff.close();
60: return true;
61: } catch (IOException e) {
62: System.out.println(“Exception: “ + e.getMessage());
63: return false;
64: }
65: }
66: }
This program's output depends on the two arguments specified when it was run. If you
use 4 and 13, the following output is shown:
Writing:
4 5 6 7 8 9 10 11 12 13
Reading:
4 5 6 7 8 9 10 11 12 13
This application consists of two classes: BufferDemo and a helper class called
ArgStream . BufferDemo gets the two arguments' values, if they are provided, and uses
them in the ArgStream() constructor.
The writeStream() method of ArgStream is called in line 14 to write the series of bytes
to a buffered output stream, and the readStream() method is called in line 16 to read
those bytes back.
 
Search WWH ::




Custom Search