I was reading through this article. It has this following snippet
OutputStream output = new FileOutputStream("c:\\data\\output-text.txt");
int data = getMoreData();
It is mentioned:
OutputStreamsare used for writing byte based data, one byte at a time. The
write()method of an
intwhich 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
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?
Each character (and almost anything stored on a file) is a byte / bytes. For example:
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 -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..
The article is incomplete, because an
OutputStream has overloaded methods for write that take a
byte along with
length arguments, or a single
In the case of writing a
String to a stream when the only interface you have is
OutputStream (say you don't know what the underlying implementation is), it would be much better to use
output.write(string.getBytes()). Iteratively peeling off a single
int at a time and writing it to the file is going to perform horribly compared to a single call to write that passes an array of bytes.
Streams operate on bytes and simply read/write raw data.
Readers and writers interpret the underlying data as strings using character sets such as UTF-8 or US-ASCII. This means they may take 8 bit characters (ASCII) and convert the data into UTF-16 strings.
Streams use bytes, readers/writers use strings (or other complex types).
The Java.io.OutputStream class is the superclass of all classes representing an output stream of bytes. When bytes are written to the OutputStream, it may not write the bytes immediately, instead the write method may put the bytes into a buffer.
There are methods to write as mentioned below:
This method writes b.length bytes from the specified byte array to this output stream.
This method writes length bytes from the specified byte array starting at offset position to this output stream.
This method writes the specified byte to this output stream.