Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Obtaining the contents of a binary file

 
Alan Jackson
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • 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!
 
Ray Stojonic
Ranch Hand
Posts: 326
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
FileInputStreamwould be a good place to start
 
jefff willis
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • 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.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • 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
  • 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
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic