permaculture playing cards*
The moose likes Java in General and the fly likes Obtaining the contents of a binary file 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 » Java in General
Bookmark "Obtaining the contents of a binary file" Watch "Obtaining the contents of a binary file" New topic
Author

Obtaining the contents of a binary file

Alan Jackson
Greenhorn

Joined: Oct 26, 2004
Posts: 26
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

Joined: Aug 08, 2003
Posts: 326
FileInputStreamwould be a good place to start
jefff willis
Ranch Hand

Joined: Sep 29, 2004
Posts: 113
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

Joined: Jul 08, 2003
Posts: 24183
    
  34

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!


[Jess in Action][AskingGoodQuestions]
Alan Jackson
Greenhorn

Joined: Oct 26, 2004
Posts: 26
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

Joined: Jul 08, 2003
Posts: 24183
    
  34

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

Joined: Oct 26, 2004
Posts: 26
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

Joined: Oct 26, 2004
Posts: 26
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 ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Obtaining the contents of a binary file
 
Similar Threads
Regarding serialization
servlet response with csv file
How to work with Binary Files?
sending pdf files to IBM MQ
Print Word Document Programatically in Java