• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

How to work with Binary Files?

 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 28371
99
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1847
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).
 
Bob Zoloman
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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).
 
Be reasonable. You can't destroy everything. Where would you sit? How would you read a tiny ad?
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic