Java Reference
In-Depth Information
public static void
main(String[] args) {
try
(
FileInputStream fis =
new
FileInputStream(
"test.dat"
); ) {
}
catch
(IOException ex) {
ex.printStackTrace();
}
catch
(FileNotFoundException ex) {
ex.printStackTrace();
}
}
}
17.18
Suppose you run the following program on Windows using the default ASCII encod-
ing after the program is finished, how many bytes are there in the file
t.txt
? Show the
contents of each byte.
public class
Test {
public static void
main(String[] args)
throws
java.io.IOException {
try
(java.io.PrintWriter output =
new
java.io.PrintWriter(
"t.txt"
); ) {
output.printf(
"%s"
,
"1234"
);
output.printf(
"%s"
,
"5678"
);
output.close();
}
}
}
17.19
After the following program is finished, how many bytes are there in the file
t.dat
?
Show the contents of each byte.
import
java.io.*;
public class
Test {
public static void
main(String[] args)
throws
IOException {
try
(DataOutputStream output =
new
DataOutputStream(
new
FileOutputStream(
"t.dat"
)); ) {
output.writeInt(
1234
);
output.writeInt(
5678
);
output.close();
}
}
}
17.20
For each of the following statements on a
DataOutputStream output
, how many
bytes are sent to the output?
output.writeChar(
'A'
);
output.writeChars(
"BC"
);
output.writeUTF(
"DEF"
);
17.21
What are the advantages of using buffered streams? Are the following statements correct?
BufferedInputStream input1 =
new
BufferedInputStream(
new
FileInputStream(
"t.dat"
));
DataInputStream input2 =
new
DataInputStream(
new
BufferedInputStream(
new
FileInputStream(
"t.dat"
)));
DataOutputStream output =
new
DataOutputStream(
new
BufferedOutputStream(
new
FileOutputStream(
"t.dat"
)));
Search WWH ::
Custom Search