问题描述:

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?

网友答案:

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

网友答案:

The article is incomplete, because an OutputStream has overloaded methods for write that take a byte[], a byte[] along with offset and length arguments, or a single int.

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:

  1. void write(byte[] b)

This method writes b.length bytes from the specified byte array to this output stream.

  1. void write(byte[] b, int position, int length)

This method writes length bytes from the specified byte array starting at offset position to this output stream.

  1. void write(int b)

This method writes the specified byte to this output stream.

相关阅读:
Top