wood burning stoves*
The moose likes I/O and Streams and the fly likes How to work with Binary Files? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "How to work with Binary Files?" Watch "How to work with Binary Files?" New topic
Author

How to work with Binary Files?

Bob Zoloman
Ranch Hand

Joined: Jul 02, 2006
Posts: 72
Hi, I am trying to make a program that can load Binary files and save Binary files, but I'm not sure on a couple of points.

1. I'm not sure which java classes to use to read in the binary file, and to save the binary file.

2. What file extension are binary files saved in?

3. What is the difference between a text file with all 1's and 0's and a binary file containing 1's and 0's?

Thanks in advance.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Originally posted by Bob Zoloman:
3. What is the difference between a text file with all 1's and 0's and a binary file containing 1's and 0's?
There is no difference. Here you have hit upon the most important point about your question: EVERY file is made up of binary 1's and 0's, regardless of whether somebody dubbed it "text" or not. So when you're saving a file, you don't need to worry about whether somebody thinks it contains text. As far as you're concerned, it's just a sequence of bytes. Treat it as binary.

There's a Sun tutorial that covers this topic in much more detail than can be fit in this little box. Start there.
Bob Zoloman
Ranch Hand

Joined: Jul 02, 2006
Posts: 72
Thanks for the explanation and link. I wrote some code that takes a small string and outputs it to a .bin file, but I'm not sure what the benefits of it are, it seems to create more output then the original test, due to all the empty box spaces before each letter. I used the FileOutputStream, and DataOutputStream.

Am I doing something wrong, or is this how it is supposed to look? If I try and cut and paste the contents of the binary file it pastes nothing.
Jeroen T Wenting
Ranch Hand

Joined: Apr 21, 2006
Posts: 1847
When you're just outputting textual data it's often easier to just use Writers and Readers and use plain text files.
But most data can't be represented like that (at least not efficiently) and that's where binary files come into their own.

Think of a file containing integer numbers. If you used a text file you'd need a lot of space for each number, and you'd need some record terminator (probably a comma or space for example).
You'd also need to do some expensive operations to convert the numbers to strings and back again on writing and reading.
When using a binary file you write the numbers directly, which always take up the same amount of space (4 bytes each for a 32 bit integer) and therefore don't need a record terminator.
You can also read and write them extremely rapidly with only a single command and no conversion needed.

When you're writing out more complex datastructures the advantages can get even bigger (which is one reason why many peoples' obsession with using XML serialisation so puzzles me).


42
Bob Zoloman
Ranch Hand

Joined: Jul 02, 2006
Posts: 72
Thanks for the explanation. I'm starting to see the advantages of using binary files. It's just hard to see the advantages by looing at the output, since it's almost identical, except for those boxes added to the letters.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Bob Zoloman:
3. What is the difference between a text file with all 1's and 0's and a binary file containing 1's and 0's?


I have the vague feeling that there is a misunderstanding about this between you and Paul.

While storing text into text files really comes down to storing bits (1's and 0's), the '1' and '0' *characters* are very different!

Those characters are encoded due to some standard, in Java UTF (-> 16 Bits), in most file systems some variant of ASCII (-> 8 Bits).

Saving a String in binary won't make much difference, because they are already encoded in binary in text files. In fact, the missing conversion from UTF to ASCII can actually make the binary files bigger than the text files, as you have noticed.

The difference comes when you are storing, for example, numbers.

The int value 1234567890 takes 64 bits in a binary file (as *every* int value does). When writing the value as a string in a text file, it would likely take 80 bits (8 for each digit). And, as Paul already explained, it also makes it harder to read the file (for the program).


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
 
Consider Paul's rocket mass heater.
 
subject: How to work with Binary Files?
 
Similar Threads
reading from a binary file
Setting the classpath
Loading Binary files
binary file to text file
Which data structure is best suited for this?