Search code examples
javaiooutputstreamfileoutputstream

clarification on using OutputStream in Java?


I was reading through this article. It has this following snippet

OutputStream output = new FileOutputStream("c:\\data\\output-text.txt");

while(moreData) {
  int data = getMoreData();
  output.write(data);
}
output.close();

It is mentioned:

OutputStreams are used for writing byte based data, one byte at a time. The write() method of an OutputStream takes an int which contains the byte value of the byte to write.

Let's say I am writing the string Hello World to the file, so each character in string gets converted to int using getMoreData() method. and how does it get written? as character or byte in the output-text.txt? If it gets written in byte, what is the advantage of writing in bytes if I have to "reconvert" byte to character?


Solution

  • Each character (and almost anything stored on a file) is a byte / bytes. For example:

    • Lowercase 'a' is written as one byte with decimal value 97.
    • Number '1' is written as one byte with decimal value 49

    There's no more concept of data types once the information is written into a file, everything is just a stream of bytes. What's important is the encoding used to store the information into the file

    Have a look at ascii table, which is very useful for beginners learning information encoding.

    To illustrate this, create a file containing the text 'hello world'

    $ echo 'hello world' > hello.txt
    

    Then output the bytes written to the file using od command:

    $ od -td1 hello.txt
    0000000  104  101  108  108  111   32  119  111  114  108  100   10
    0000014
    

    The above means, at address 0000000 from the start of the file, I see one byte with decimal value 104 (which is character 'h'), then one byte with decimal value 101 (which is character 'e") and so on..