Take the 2-minute tour ×

I Know that binary files connot be read by text editors, thay can be read only by programs. but when I create binary files using java(bytes), I can open files and read it ? why that happens? In other words, I see a plain text rather than a sequence of zeros and ones. I know that in java there are byte-based streams and character-based streams. when I use byte-based streams such as FileOutputStream, the output is characters not bytes.

    File file = new File("Myfile.dat"); // .txt or .bin
    FileOutputStream fos = new FileOutputStream(file);
    String data = "Hello, world";

when I open Myfile.dat using notepad, I expect to see special characters or 0s and 1s but I can read the content "hello world". So I do not undrstand how byte-based streams store characters in binary format?

share|improve this question

closed as not a real question by AVD, Matt Ball, JB Nizet, home, Paŭlo Ebermann Sep 26 '11 at 23:55


Your question is too vague to answer at the moment. Please read tinyurl.com/so-hints –  Jon Skeet Sep 26 '11 at 13:01
What do you mean by "read it"? Are you seeing plaintext or a bunch of nonsense characters? –  John Giotta Sep 26 '11 at 13:02
Every file is "binary." It's only a matter of how the zeros and ones are interpreted. –  Matt Ball Sep 26 '11 at 13:02
I edit the question, please reread it. –  sahar Sep 26 '11 at 20:00
@sasola Because you're writing ASCII (not quite true, but for the purposes of this discussion, close enough), which text editors can understand. Again--all files are binary. A 0b01101001 is 65 decimal, which is a capital "A". A text editor understands an "A", so it displays fine. –  Dave Newton Sep 26 '11 at 20:29

3 Answers 3

It's not that "binary files" can't be read by text editors, it's that "binary files containing characters useless to a text editor can't be read/edited in a meaningful way". After all, all files are binary: it just depends on what's contained in them.

share|improve this answer
I edit the question and add more details, you can take a look –  sahar Sep 26 '11 at 20:07

Binary files can be opened by most text editors too, it just depends on what the binary values are and how the editor interpret them as text values. There is no reason why you couldn't open the output of your Java program in Notepad or any simple text editor - you'll just see gibberish and special characters! If you wan to see the contents as binary then you'll need a binary file editor/viewer such as hexEdit.

share|improve this answer

What exactly do you mean by "binary file"? There is no magic tag that marks a file as either a "binary file" or a "text file" (at least not in any OS in common use these days).

A file is nothing but a stream of bytes with a name and some metadata (creation date, permissions, ...) attached.

Whether the content can be interpreted as text or bytes depends entirely on the content.

So if you write a file only using binary values less than 128 (and avoid some low values), then the result is valid ASCII and might look (roughly) like a text file when opened in a text editor.

If, however, you write random bytes to a file, then opening it with a text editor will not result in sane output.

share|improve this answer
I edit the question and add more details, you can take a look. –  sahar Sep 26 '11 at 20:02
