Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Reading data from files

 
sankar dunga
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 276
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
sankar dunga
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20669
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
sankar dunga
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you.
I will try in this way.


regards
sankar
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic