Java Reference
In-Depth Information
(a)
(b)
F
IGURE
17.21
(a) The program splits a file. (b) The program combines files into a new file.
*17.13
(
Combine files GUI
) Rewrite Exercise 17.12 with a GUI, as shown in
FigureĀ 17.21b.
17.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.
17.15
(
Decrypt files
) Suppose a file is encrypted using the scheme in Programming
Exercise 17.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.
17.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.
**17.17
(
BitOutputStream
) Implement a class named
BitOutputStream
, as shown
in FigureĀ 17.22, 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 Exercise 5.44. Write a test program that
sends the bits
010000100100001001101
to the file named
Exercise17_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
17.22
BitOutputStream
outputs a stream of bits to a file.
*17.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)
For a hint, see Programming Exercise 5.44. Write a program that prompts the
user to enter a file name, reads bytes from the file, and displays each byte's binary
representation.
Search WWH ::
Custom Search