Java Reference
In-Depth Information
java.io.OutputStream
java.io.FilterOutputStream
java.io.BufferedOutputStream
+BufferedOutputStream(out: OutputStream)
Creates a
BufferedOutputStream
from an
OutputStream
object.
Creates a
BufferedOutputStream
from an
OutputStream
object with specified size.
+BufferedOutputStream(out: OutputStream, bufferSize: int)
F
IGURE
17.14
BufferedOutputStream
buffers an output stream.
If no buffer size is specified, the default size is
512
bytes. You can improve the perfor-
mance of the
TestDataStream
program in Listing 17.2 by adding buffers in the stream in
lines 6-7 and lines 19-20, as follows:
DataOutputStream output =
new
DataOutputStream(
new
BufferedOutputStream(
new
FileOutputStream(
"temp.dat"
)));
DataInputStream input =
new
DataInputStream(
new
BufferedInputStream(
new
FileInputStream(
"temp.dat"
)));
Tip
You should always use buffered I/O to speed up input and output. For small files, you
may not notice performance improvements. However, for large files—over 100 MB—
you will see substantial improvements using buffered I/O.
17.8
Why do you have to declare to throw
IOException
in the method or use a try-catch
block to handle
IOException
for Java I/O programs?
17.9
Why should you always close streams? How do you close streams?
17.10
The
read()
method in
InputStream
reads a byte. Why does it return an
int
instead of a
byte
? Find the abstract methods in
InputStream
and
OutputStream
.
17.11
Does
FileInputStream
/
FileOutputStream
introduce any new methods beyond
the methods inherited from
InputStream
/
OutputStream
? How do you create a
FileInputStream
/
FileOutputStream
?
✓
✓
Check
Point
17.12
What will happen if you attempt to create an input stream on a nonexistent file? What
will happen if you attempt to create an output stream on an existing file? Can you
append data to an existing file?
17.13
How do you append data to an existing text file using
java.io.PrintWriter
?
17.14
Suppose a file contains an unspecified number of
double
values that were written to
the file using the
writeDouble
method using a
DataOutputStream
, how do you
write a program to read all these values? How do you detect the end of a file?
17.15
What is written to a file using
writeByte(91)
on a
FileOutputStream
?
17.16
How do you check the end of a file in an input stream (
FileInputStream
,
DataInputStream
)?
17.17
What is wrong in the following code?
import
java.io.*;
public class
Test {
Search WWH ::
Custom Search