Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I read/write bits from/to a file in Java?

I need to read file stream as bits and then I should be able to write bits to file again. Are there any classes for this purpose?

like image 458
newbie Avatar asked Dec 03 '09 15:12

newbie


People also ask

How to read and write files in Java?

Note: There are many available classes in the Java API that can be used to read and write files in Java: FileReader, BufferedReader, Files, Scanner, FileInputStream, FileWriter, BufferedWriter, FileOutputStream, etc.

How do I read and write binary data in Java?

The utility class Files in the java.nio.file package provides the following methods for reading and writing binary data: readAllBytes(Path path): reads all bytes from a file and returns an array of bytes. This method is intended for reading small files, not large ones.

How do you read and write bits in C++?

To read a bit you'll need to read a byte and then use bit manipulation to inspect the bits you care about. Likewise, to write bits you'll need to write bytes containing the bits you want.

What is the difference between read() and write() methods in Java IO?

Using these few classes, you can easily interact with files in a more efficient way. The main difference between these two packages is that the read () and write () methods of Java IO are a blocking calls. By this we mean that the thread calling one of these methods will be blocked until the data has been read or written to the file.


1 Answers

As far as I know, there is no built in way to do it directly at the bit level. There are ways to read/write byte by byte using the built in streams. You could write a wrapper on a stream that looks like it reads and writes bits.

If you want something that is already written, look for open source projects that do certain kinds of audio or video encoding, since there are things like that. For instance, the FLAC codec had a BitInputStream that might meet your needs.

like image 183
Uri Avatar answered Oct 01 '22 12:10

Uri