5

This is a basic question.

When I use a byte stream to write bytes to a file, am I creating a binary file?

For example: I use a byte stream to write text data to a notepad and when I open the notepad in a HEX viewer I see the corresponding hex value for each character. But why not the binary values (i.e 0s and 1s).

I also learned that using a dataoutput/input stream I read/write binary file.

I guess my confusion is with what does it mean to write bytes and what does it mean to write a binary data.

1
  • Making it binary would make it 8 times big. Why? You ask about expanding a byte to 8-bits then write those bits as '1' and '0' chars? Commented Sep 4, 2012 at 18:07

4 Answers 4

4

When I use a byte stream to write bytes to a file, am I creating a binary file?

You write the bytes as is, e.g., as the ones and zeroes they are. If these bytes represents characters then commonly no, it's just a text-file (everything is ones and zeroes after all). Otherwise the answers is it depends. The term binary file is missleading, but is usually referers to as a file which can contain arbitrary data.

when I open the notepad in a HEX viewer I see the corresponding hex value for each character. But why not the binary values

HEX is just another representation of bytes. The following three are equal

10       (Decimal value 10)
0xA      (Hex value 10)
00001010 (Binary value 10)

A computer only stores binary values. But editors may choose to represent (display) those in another way, such as Hex or decimal form. Given enough bytes, it can even be represented as an image.

what does it mean to write bytes and what does it mean to write a binary data

Binary data means ones and zeroes, e.g., 00001010 which are 8 bits. 8 bits makes a byte.

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

3 Comments

No worries, your answer is by far the most correct, clear and (hopefully) helpful here. +10 if I could :)
Thanks Johan.. So the point is irrespective of whether I use a character stream or a byte stream, if I am writing an entirely text data then it would be a text file and if it is not entirely text data then it is a binary file. So, this makes me ask one more question that, is it always better to use a character stream to read/write text data? Is writing as bytes requires less space/memory?
@user547453, character streams are often wrappers for byte streams, so use the implementation which API best fits your program.
1

The confusion could be caused by the application you are using. If you open something in HEX viewer, it should be represented in HEX not BIN.

Comments

1

The notions of "text" and "binary" files is mostly a notional understanding for you and me as "consumers" of the file. Strictly speaking, every file consists of 1's and 0's, and are thus all binary in the truest sense of the word. Hexadecimal representations, encodings for a particular character set, image file formats. You can spin up an array of 100 random bytes, spit it out to a file, and it's just as "binary" as any other file. Its all in the context of how the bytes are interpreted that makes the difference.

Here's an example. In old tried-and-true ACII, an upper-case "A" is encoded as decimal 65. You can represent that to people as 0x41 (hex) in a hex viewer, as an "A" an editor, but ultimately, you write that byte to a file, it's just a byte translated to a series of eight bits, 01000001.

Comments

1

Typically you are create a text file using Writer(s), and a binary file using other means (Streams, Channels, etc.). However, if your 'binary' file contains text and only text, it is a text file regardless.

Regarding hexadecimal format, that is merely a compact (preferred) way of viewing byte values.

6 Comments

There is no such thing as a binary file or text file. There are files. Period. (But there are also different ways to work with a file.)
cplusplus.com/doc/tutorial/files File streams opened in binary mode perform input and output operations independently of any format considerations. Non-binary files are known as text files, and some translations may occur due to formatting of some special characters (like newline and carriage return characters).
Your point being? The term is obviously in use, but that does not make it any better.
The author was referring to binary/text file differences in the C/C++ sense. There is no point telling the author that a file is just a logical sequence of data, because that is definitely not what the question was about.
There is not a single mention of C++ anywhere. OP uses Java, but even then half of the question is about how data is stored in files, not about what terms some APIs use. Apart from that, the terms are binary/text mode, which is much more correct.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.