Question: Implement a class named BitOutputStream for writing a stream of bits to a file as follows: +BitOutputStream(file: File) // Creates a BitOutputStream to write bits

Implement a class named BitOutputStream for writing a stream of bits to a file as follows:

+BitOutputStream(file: File) // Creates a BitOutputStream to write bits to the file.

+writeBit(char bit): void // Writes a bit '0' or '1' to the output stream

+writeBit(String bit): void // Writes a string of bits to the output stream

+close(): void // This method must be invoked to close the 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 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 a byte is neither empty nor full, the close() method first fills in zeros to make a full 8 bits in the byte, and then closes the stream. Hint: It might help to look at Exercise 5.44, on page 202 (Introduction to Java Programming and Data Structures), as well as Appendix G on Bitwise operations on page 1169 of the text.

Write a program that sends the bits 010000100100001001101 to a file called testOutput.dat.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!