Java Reference
In-Depth Information
(a)
(b)
F
IGURE
19.20
(a) The program splits a file. (b) The program combines files into a new file.
*19.13
(
Combine files GUI
) Rewrite Exercise 19.12 with a GUI, as shown in Figure
19.20b.
19.14
(
Encrypt files
) Encode the file by adding
5
to every byte in the file. Write a pro-
gram that prompts the user to enter an input file name and an output file name
and saves the encrypted version of the input file to the output file.
19.15
(
Decrypt files
) Suppose a file is encrypted using the scheme in Programming Exer-
cise 19.14. Write a program to decode an encrypted file. Your program should
prompt the user to enter an input file name for the encrypted file and an output file
name for the unencrypted version of the input file.
19.16
(
Frequency of characters
) Write a program that prompts the user to enter the
name of an ASCII text file and displays the frequency of the characters in the file.
**19.17
(
BitOutputStream
) Implement a class named
BitOutputStream
, as shown
in Figure 19.21, for writing bits to an output stream. The
writeBit(char
bit)
method stores the bit in a byte variable. When you create a
BitOutputStream
, the byte is empty. After invoking
writeBit('1')
, the
byte becomes
00000001
. After invoking
writeBit("0101")
, the byte
becomes
00010101
. The first three bits are not filled yet. When a byte is full, it
is sent to the output stream. Now the byte is reset to empty. You must close the
stream by invoking the
close()
method. If the byte is neither empty nor full,
the
close()
method first fills the zeros to make a full
8
bits in the byte, and
then outputs the byte and closes the stream. For a hint, see Programming Exer-
cise 4.46. Write a test program that sends the bits
010000100100001001101
to the file named
Exercise19_17.dat
.
BitOutputStream
+BitOutputStream(file: File)
+writeBit(char bit): void
+writeBit(String bit): void
+close(): void
Creates a
BitOutputStream
to writes bits to the file.
Writes a bit '
0
' or '
1
' to the output stream.
Writes a string of bits to the output stream.
This method must be invoked to close the stream.
F
IGURE
19.21
BitOutputStream
outputs a stream of bits to a file.
*19.18
(
View bits
) Write the following method that displays the bit representation for
the last byte in an integer:
public static
String getBits(
int
value)