How to use Java to read a binary file?

W

www

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.
 
R

Richter~9.6

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.

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

Regards,
Richard
 
A

Alex Hunsley

www said:
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
 
T

Tris Orendorff

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().
 
M

Mark Rafn

www said:
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 :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,273
Latest member
DamonShoem

Latest Threads

Top