aspose file tools*
The moose likes Java in General and the fly likes Reading data from files Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Reading data from files" Watch "Reading data from files" New topic
Author

Reading data from files

sankar dunga
Greenhorn

Joined: Mar 07, 2011
Posts: 16
Hello sir,

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.

can you help me how to do this.

Regards
sankar
Vinoth Kumar Kannan
Ranch Hand

Joined: Aug 19, 2009
Posts: 276

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
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: 42919
    
  68
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.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19784
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
sankar dunga
Greenhorn

Joined: Mar 07, 2011
Posts: 16
Thank you.
I will try in this way.


regards
sankar
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Reading data from files