• 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:

Obtaining the contents of a binary file

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Im wanting to obtain the contents of binary files, but honestly don't know where to begin. I can open and read txt files, can anyone give me some help on how to obtain the contents of a binary file?

Cheers!
 
Ranch Hand
Posts: 326
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
FileInputStreamwould be a good place to start
 
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Read them into a byte array.

Here is a method that will do that for you:




Just pass it the File.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Welcome to JavaRanch!

You may not have read our naming policy on the way in. It reqiures that you use a full, real (sounding) first and last name for your display name -- no "handles" or such. You can change your display name here. Thanks, pardner!
 
Alan Jackson
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Name changed.

Thanks for the code jefff. What would the results be if I were to read in say an xls file, or a doc file?
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In every case, the results are the same: you'll get an array of bytes, and then it's up to you to somehow interpret those bytes. There are specialized libraries for interpreting those bytes in various kinds of binary files; in general, each type is going to mean using a different library to understand it.

What are you wanting to do, specifically?
 
Alan Jackson
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To be honest, I just wanna output the bytes from the file but if its a textfile I wanna read in the contents of the file.

Im still having problems getting it working. Where would I find an example program which simply shows the file contents via the console? You know, like:

read bytes
display bytes
end program
[ November 04, 2004: Message edited by: Alan Jackson ]
 
Alan Jackson
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay, Ive managed to get the code compiling except from one bit...

For some odd reason, I cannot figure out how to call the method. Ive been using:

fileBytes = getBytesFromFile("file.txt");

Should I be doing something else? The error Im getting is:
fileBytes = getBytesFromFile("file.txt"); Cannot use java.lang

With the little ^ pointing to the 'g' in get

Any ideas?
[ November 04, 2004: Message edited by: Alan Jackson ]
 
A timing clock, fuse wire, high explosives and a tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic