What is the difference between a byte stream and a character stream && is it better to use a word throttle or a character flow?

Source: Internet
Author: User

byte stream :

A FileOutputStream (file name) creates a file output stream that outputs data to the specified file object.

B FileOutputStream (filedescriptor) creates a file output stream that outputs data to the specified file descriptor.

C FileOutputStream (String name) creates a file output stream that outputs data to a file of the specified name.

D FileOutputStream (String, Boolean) creates an output file with the file name of the specified system.

character Stream :

InputStreamReader and Outputstreamreader:

Converts a byte-oriented stream into a character-oriented stream.

The InputStreamReader class is a bridge that flows from a byte to a character stream: it reads in bytes and converts it to a character stream based on the specified encoding.

The encoding used may be specified by name, or the platform can accept the default encoding method.

Each invocation of one of the InputStreamReader's read () methods may cause one or more bytes to be read from the base byte input stream.

In order to achieve greater efficiency, consider packaging inputstreamreader with BufferedReader,

BufferedReader in = new BufferedReader (new InputStreamReader (system.in));

Same point:

Inputstream,outputstream,reader,writer are abstract classes. So you can't just new

The difference between a byte stream and a character stream

The byte stream is very similar to the use of character streams, so what is the difference between the operation code and the other?

Difference:

1, the byte stream in the operation of the time itself is not used in the buffer (memory), is directly manipulated with the file itself, and the character stream is used in the operation of the buffer

2. Byte stream when you manipulate a file, the file can output even if the resource is not closed (the Close method), but if the character stream does not use the Close method, nothing is output, the character stream uses a buffer, and the flush method can be used to force the flush buffer. Then you can output the content without close.

3. The read () method of the Reader class returns the type int: The character read as an integer (two bytes total 16 bits), the range is between 0 and 65535 (0X00-0XFFFF), and returns 1 if the end of the stream has been reached.
The read () of InputStream also returns int, but since this class is byte-oriented and has 8 bits for one bytes, it returns an int byte value in the range of 0 to 255 . If no bytes are available since the end of the stream has been reached, the return value-1. So for values that cannot be represented by 0-255, a character stream is used to read it! such as Chinese characters.

4. The main difference between byte stream and character stream is the way they handle it.

Byte stream: Handles bytes and byte arrays or binary objects;

Character Stream: Handles characters, character arrays, or strings.

In the development of the use of the word throttle or use character flow good?

Characters (reader and Writer): Chinese, characters are only formed in memory, manipulating characters, arrays of characters, or strings,

Two, bytes (InputStream and OutputStream): Audio files, pictures, songs, all the hard disk to save files or transfer, the operation of bytes and byte arrays or binary objects,

* If you want a Java program to implement a copy function, you should use the byte stream operation (may be copied pictures), and the use of side-reading side write (save memory).

***

* * Buffer

A buffer can be simply understood as a region of memory.
You can simply interpret the buffer as a special memory.
In some cases, if a program frequently operates a resource (such as a file or database), the performance is very low, at this point in order to improve performance, you can temporarily read some of the data into the memory of a piece of the area, and then directly from the region to read the data, because the read memory speed will be faster, This can improve the performance of the program.
In a character stream operation, all characters are formed in memory, and all of the content is temporarily stored in memory before output, so buffer staging data is used.
You can use the flush () method in the writer class if you want to output all the contents of a character stream without closing it.

What is the difference between a byte stream and a character stream && is it better to use a word throttle or a character flow?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.