Re: How to use Java to read a binary file?



On 16 Feb, 17:07, www <w...@xxxxxxxxxx> wrote:
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

.



Relevant Pages

  • How to use Java to read a binary file?
    ... I have a binary file. ... They could be numbers, or chars. ... I am trying to use Java to read it and print out to the screen. ... public class CopyBytes { ...
    (comp.lang.java.programmer)
  • Re: How to use Java to read a binary file?
    ... I am trying to use Java to read it and print out to the screen. ... 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'. ...
    (comp.lang.java.programmer)
  • Re: how to wait for socket communications
    ... chars or bytes out of it? ... how to communicate the chars to Java. ... to use socket communication between my C++ program and my Java program. ...
    (microsoft.public.win32.programmer.networks)
  • Re: how to wait for socket communications
    ... Well, in C++ I am converting the unsigned chars to chars, but I don't know ... how to communicate the chars to Java. ... to use socket communication between my C++ program and my Java program. ...
    (microsoft.public.win32.programmer.networks)
  • Re: how to wait for socket communications
    ... how to communicate the chars to Java. ... because it seems that C++ chars are the same as Java bytes. ... So if I can just make a file out of that, the communication from C++ ... to use socket communication between my C++ program and my Java program. ...
    (microsoft.public.win32.programmer.networks)