-1

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";
    fos.write(data.getBytes());
    fos.close();

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?

sahar
  • 569
  • 7
  • 16
  • 29
  • Your question is too vague to answer at the moment. Please read http://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
  • 7
    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
  • @DaveNewton please read sec 14.3 Files and Streams in Java How to Program 7e, specialy the last sentence: '_Files that are created using byte-based streams are referred to as binary files, while files created using character-based streams are referred to as text files. Text files can be read by text editors, while binary files are read by a program that converts the data to a human-readable format._' This made me confused :( – sahar Sep 26 '11 at 21:21
  • Again, text files are still binary: that some binary data may be understood by a text editor is coincidental in the sense that some binary data happens to be ASCII. – Dave Newton Sep 26 '11 at 21:36

3 Answers3

6

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.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
1

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.

Ashkan Aryan
  • 3,504
  • 4
  • 30
  • 44
1

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.

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614