Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / February 2007

Tip: Looking for answers? Try searching our database.

How to use Java to read a binary file?

Thread view: 
www - 16 Feb 2007 17:07 GMT
Hi,

I have a binary file. I have no idea what is inside. They could be
numbers, or chars. I am trying to use Java to read it and print out to
the screen. Following is my program:

<Java>
import java.io.*;

public class CopyBytes {
    public static void main(String[] args) throws IOException {
        DataInputStream in = null;
        try {
            in = new DataInputStream(new
FileInputStream("binary_file_name"));
            char c;

            while ((c = in.readChar()) != -1) {
                System.out.print(c);
            }

        } finally {
            if (in != null) {
                in.close();
            }
        }
    }
}
</Java>

The print out to screen is very, very messy. I saw Korean letters,
Chinese letters printed out. I am wondering if I did it right.

Thank you for your help.
Richter~9.6 - 16 Feb 2007 17:21 GMT
> Hi,
>
[quoted text clipped - 30 lines]
>
> Thank you for your help.

Are you trying to print out the binary value? Give
System.out.printf("Binary value: %b", c) a try.

Regards,
Richard
Alex Hunsley - 16 Feb 2007 17:56 GMT
> Hi,
>
> I have a binary file. I have no idea what is inside. They could be
> numbers, or chars. I am trying to use Java to read it and print out to
> the screen. Following is my program:

Essentially a file is a sequence of numbers (bytes). You can interpret
those numbers in any way you like - as chars, as plain bytes, as a
serialised object of some sort, however you like.
There's not a standard way to ask a binary file what it actually
'contains'.
You need to have some idea of what the file you are reading contains for
it to be very useful....
HTH,
lex
Tris Orendorff - 16 Feb 2007 18:57 GMT
> I have a binary file. I have no idea what is inside. They could be
> numbers, or chars. I am trying to use Java to read it and print out to
> the screen. Following is my program:

If you are trying to do a binary read of a file then you should
use FileInputStream and read() and dispense with
DataInputStream and readChar().

Signature

Tris Orendorff
[Q: What kind of modem did Jimi Hendrix use?
A: A purple Hayes.]

Mark Rafn - 16 Feb 2007 19:59 GMT
>I have a binary file. I have no idea what is inside.

Ok.

>They could be numbers, or chars.

So you have some idea what's inside.  There are distinct things (defined
somehow) that could be numbers or chars.

>I am trying to use Java to read it and print out to
>the screen. Following is my program:

>             in = new DataInputStream(new
>FileInputStream("binary_file_name"));

Why are you using a DataInputStream?  That only works for files that were
written by a DataOutputStream, and requires you to know what's there.

>             while ((c = in.readChar()) != -1) {

How do you know it's a char?  You said it could be numbers or chars...

If it's truly a binary file and you don't know anything about the contents,
use a FileInputStream and read bytes.  Then interpret the bytes based on
whatever rules you have for the data.

If it's a file where you know more about the format, tell us that so we can
help more specifically :)
--
Mark Rafn    dagon@dagon.net    <http://www.dagon.net/>


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.