Byte (byte), binary, character stream, character stream related concepts analysis

Source: Internet
Author: User
Tags arrays character set flush

1. Bytes:

The word (Byte) section is the length unit. Bit (bit) is also the unit of length.

Because computer communication and storage is based on 010101 of such binary data, where a 0 and 1 of the place is called bit (bit), that is, a bits.

1byte=8bit

1kb=1024b

1MB=1024KB (2 of 10 Parties)

2. Binary

Binary number has two features: it is composed of two basic character 0,1, binary number operation law is every two into one.

In order to differentiate from other binary numbers, the writing of the binary number is usually in the lower right of the number of the Radix 2, or plus the following plus B is indicated.

For example: binary number 10110011 can be written as (10110011) 2, or write 10110011B, for the decimal number can not be raised. The data in the computer is represented by a binary number, because the binary number has the following characteristics:

1) There are only two characters in the binary number 0 and 1, indicating that there are two different stable states of the components. For example, in the circuit, there is no current, there is a current with 1, no current with 0 means. Similar to the voltage in the circuit of high, low, transistor conduction and cutoff.

2) Binary number operation is simple, greatly simplifies the calculation of the structure of the operational components.

The addition and multiplication of binary numbers are as follows:

0+0=0 0+1=1+0=1 1+1=10

0x0=0 0x1=1x0=0 1x1=1

Because the binary number in the use of the median number is too long, not easy to remember, so the hexadecimal number is proposed.

3. Byte stream and character stream

Concept

All the data in the program is transmitted or saved in a stream, and the program needs the data to read the data using the input stream, and when the program needs to save some data, it will use the output stream to complete.

The input and output in the program is saved as a stream, and the actual bytes stored in the stream are all byte files.

Byte Stream and character stream

In the java.io package to manipulate the contents of the main two major categories: byte stream, character stream, two categories are divided into input and output operations. Output data in the byte stream is mainly done using OutputStream, the input is InputStream, the output in the character stream is mainly done using the writer class, and the input stream is mainly done using reader class. (These four are abstract classes)

The package java.io, which is dedicated to the input and output functions, is available in Java, including:

Inputstream,outputstream,reader,writer

InputStream and OutputStream, two are designed for byte streams, primarily for processing bytes or binary objects,

Reader and Writer. Two are designed for character streams (two bytes in a character) and are primarily used to handle characters or strings.

A character stream processes a cell that is 2-byte Unicode characters, manipulating characters, character arrays, or strings, and a byte-stream processing unit of 1 bytes, manipulating bytes and byte arrays. So a character stream is a character in which a Java Virtual machine translates bytes into 2-byte Unicode characters, so it is better for multi-lingual support. If it is audio files, pictures, songs, use a word throttle better, if it is related to Chinese (text), with a character stream better

All of the files are stored in byte (byte) storage, and the characters that are not files on the disk are encoded into bytes, and then the bytes are stored to disk. When reading a file (especially a text file), it is also a byte-by-byte read to form a sequence of bytes

Byte streams can be used for any type of object, including binary objects, whereas character streams only handle characters or strings; 2. Byte stream provides the ability to handle any type of IO operation, but it cannot handle Unicode characters directly, and a character stream can

Byte stream is the most basic, all the Inputstrem and OutputStream subclasses are, mainly used in processing binary data, it is processed by byte, but in fact, a lot of data is text, and put forward the concept of character stream, it is the virtual machine encode to deal with, That is, the conversion of the character set is related by Inputstreamreader,outputstreamwriter, in fact through byte[] and string. The problem of Chinese characters appearing in the actual development is actually caused by the transformation of the character stream and the byte stream.


Difference

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 different than manipulating the code?

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

Byte stream when manipulating 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 is buffered, and the flush method can be used to force the flush buffer. Then you can output the content without close.

In the development of whether to use the word throttle or with the character flow good.

When you save files on all your hard disks or transfer them in a byte-by-byte manner, including the picture is done in bytes, and the characters are only formed in memory, so the use of bytes is the most.

If you want a Java program to implement a copy function, you should use the byte stream to operate (possibly copying the picture), and to use the side-read-write Method (save memory).

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.