I have binary file.
It continuous bits inform of binary digits.
I want read every 8 bits(1 byte) of data once, and then want store that into a another string, to write into a output file.
Java has built-in I/O APIs that can do what you want to do.
Basically, what you need to do is create an input stream that can read from a file, and invoke read() method on it. Then, you must open an output stream that can write to a file, and invoke write() method on it with proper parameters.
If you have not been exposed to java I/O, I'd suggest reading some tutorial on this. That can get you going.
OCPJP 6
sankar dunga
Greenhorn
Joined: Mar 07, 2011
Posts: 16
posted
0
Yah , i know some functions to read from and write data to files.
But i want to read only 8 bits(binary data) of data at a time, and then want to convert it into ascii.
Then i want to write it into another file.
How can we do it?
If you know any code for this, send me please.........
thank you
Ulf Dittmer
Marshal
Joined: Mar 22, 2005
Posts: 35253
7
posted
0
i want to read only 8 bits(binary data) of data at a time, and then want to convert it into ascii.
The FileInputStream class has a method for reading single bytes. I assume that you know contents of the file, so that you can properly convert bytes to characters, taking into account encodings and such.
8 bits is one byte, and that's exactly what is returned by InputStream.read(). Yes, the data type is int, but that's only to allow all possible byte values plus an additional value that indicates "nothing left". So to read one single byte:
After that you can do anything you want with that byte. But as you were told in your previous thread, you can't just cast any byte to a character. You need some other transformation mechanism; the other thread already mentioned base64, but simple HEX encoding (where 1 byte takes up two characters, from "00" to "FF") can also be used. There's been a thread around here somewhere where some people posted code to convert a byte[] into a HEX string. Look it up.