Java Reference
In-Depth Information
InputStream defines three basic methods of reading data:
Method
Description
read()
Abstract method that reads a single byte and returns its value as an
integer
read(byte [] data)
Reads bytes to fill the byte array and returns the number of bytes read
read(byte [] data,
Reads bytes to fill the byte array at the selected point and returns the
int offset, int len)
number of bytes read
The classes that extend InputStream will override these methods to handle the un-
derlying storage or transport media. A FileInputStream will read data from disk. An
InputStream obtained from a network connection will read data from a TCP/IP con-
nection. The logic for performing the reading remains essentially the same because in
both cases the logic is using the InputStream methods for reading and writing.
Method
Description
write(int b)
Abstract method that writes a single byte
write (byte [] data)
Writes all the bytes from the byte array
write(byte [] data,
Writes selected bytes from the byte array
int offset, int len)
OutputStream parallels InputStream in having three basic methods for writing data:
To illustrate the usage of InputStream s and OutputStream s, I am going to extend
the earlier example and create a simple file-copy program. A simple file-copy pro-
gram in COBOL would look something like this:
FD INPUT-FILE.
01 INPUT-RECORD.
05 FILLER PIC X(100).
FD OUTPUT-FILE.
01 OUTPUT-RECORD.
05 FILLER PIC X(100).
OPEN INPUT INPUT-FILE.
OPEN OUTPUT OUTPUT-FILE.
 
Search WWH ::




Custom Search