Java Reference
In-Depth Information
java.io.InputStream
+
read(): int
Reads the next byte of data from the input stream. The value byte is returned as
an
int
value in the range
0
to
255
. If no byte is available because the end of
the stream has been reached, the value -1 is returned.
Reads up to
b.length
bytes into array
b
from the input stream and returns the
actual number of bytes read. Returns -1 at the end of the stream.
Reads bytes from the input stream and stores them in
b[off], b[off+1], . . .,
b[off+len-1]
. The actual number of bytes read is returned. Returns -1
at the end of the stream.
Returns an estimate of the number of bytes that can be read from the input stream.
Closes this input stream and releases any system resources occupied by it.
Skips over and discards
n
bytes of data from this input stream. The actual
number of bytes skipped is returned.
Tests whether this input stream supports the
mark
and
reset
methods.
Marks the current position in this input stream.
Repositions this stream to the position at the time the
mark
method was last
called on this input stream.
+read(b: byte[]): int
+read(b: byte[], off: int,
len: int): int
+available(): int
+close(): void
+skip(n: long): long
+markSupported(): boolean
+mark(readlimit: int): void
+reset(): void
F
IGURE
17.4
The abstract
InputStream
class defines the methods for the input stream of bytes.
binary input classes, and
OutputStream
is the root for binary output classes. Figures 17.4
and 17.5 list all the methods in the classes
InputStream
and
OutputStream
.
Note
All the methods in the binary I/O classes are declared to throw
java.io.IOException
or a subclass of
java.io.IOException
.
throws IOException
java.io.OutputStream
+
write
(
int b
)
: void
Writes the specified byte to this output stream. The parameter
b
is an
int
value.
(byte)b
is written to the output stream.
+write(b: byte[]): void
Writes all the bytes in array
b
to the output stream.
+write(b: byte[], off: int,
len: int): void
Writes
b[off], b[off+1],. . ., b[off+len-1]
into the output stream.
+close(): void
+flush(): void
Closes this output stream and releases any system resources occupied by it.
Flushes this output stream and forces any buffered output bytes to be written out.
F
IGURE
17.5
The abstract
OutputStream
class defines the methods for the output stream of bytes.
17.4.1
FileInputStream
/
FileOutputStream
FileInputStream
/
FileOutputStream
is for reading/writing bytes from/to files.
All the methods in these classes are inherited from
InputStream
and
OutputStream
.
FileInputStream
/
FileOutputStream
does not introduce new methods. To construct a
FileInputStream
, use the constructors shown in Figure 17.6.
A
java.io.FileNotFoundException
will occur if you attempt to create a
FileInputStream
with a nonexistent file.
To construct a
FileOutputStream
, use the constructors shown in Figure 17.7.
If the file does not exist, a new file will be created. If the file already exists, the first two
constructors will delete the current content of the file. To retain the current content and append
new data into the file, use the last two constructors and pass
true
to the
append
parameter.
FileNotFoundException
Search WWH ::
Custom Search