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/>