0

I am currently learning Java and I need some help creating a binary file. My assignment is to take a textfile, create a binary search tree using the huffman algorithm, and create a textfile with the resulting binary digits.

How I can actually create a binary file with those binary digits instead of the regular binary digits associated with characters?

James A Mohler
  • 11,060
  • 15
  • 46
  • 72
  • 5
    *"Sorry if this question is dumb"* What 'question'? This is a homework dump. – Andrew Thompson Nov 28 '13 at 00:37
  • 1
    I've completed the assignment. I'm trying to do something that I haven't learned, which is why I'm asking the question.. – user3043950 Nov 28 '13 at 00:44
  • 1
    @AndrewThompson Disagree. This is an intelligent question, just the terminology is strange. It is actually: How do I write 0x55 instead of the 8 character string "01010101". – Ingo Nov 28 '13 at 01:06
  • When you say "textfile with the resulting binary digits", you're being confusing. Do you actually mean a text file? Do you want to output the raw bytes, or are you encoding them as text (like writing a number as the string "123" instead of a byte)? Please be very specific about what output you are trying for. – chrylis -cautiouslyoptimistic- Nov 28 '13 at 01:25
  • 1
    Do you have any of the code that you used for the text file? What have you tried in order to make it a binary file? – James A Mohler Nov 28 '13 at 01:25
  • I have a string of 0s and 1s. I want to turn that into bytes. So I want every 8 numbers to be a byte, and then create a file with those bytes. – user3043950 Nov 28 '13 at 01:41

1 Answers1

2

Short answer: You group your binary digits (also known as bits) in units of bytes, and then you write those bytes. You need a class that supports writing binary data (instead of text data), like for example FileOutputStream.

BTW, contrary to the opinion of others, this is not a dumb question! The question shows that you have thought about how data are represented in files and how you should go about it.

Ingo
  • 36,037
  • 5
  • 53
  • 100