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

7
  • Your question is too vague to answer at the moment. Please read tinyurl.com/so-hints Commented Sep 26, 2011 at 13:01
  • What do you mean by "read it"? Are you seeing plaintext or a bunch of nonsense characters? Commented Sep 26, 2011 at 13:02
  • 7
    Every file is "binary." It's only a matter of how the zeros and ones are interpreted. Commented Sep 26, 2011 at 13:02
  • I edit the question, please reread it. Commented Sep 26, 2011 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. Commented Sep 26, 2011 at 20:29

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I edit the question and add more details, you can take a look
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.

1 Comment

I edit the question and add more details, you can take a look
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.

1 Comment

I edit the question and add more details, you can take a look.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.