IO in Java learning retention

Basic concepts

Bits, bytes, the distinction between the three characters.

Bit (bit): is the minimum unit of data stored inside the computer, is an eight-bit binary number 11001100.

Byte (byte): is the basic unit of computer data processing, denoted by capital B Traditionally, 1B (byte, byte) = 8bit (bits)

Character: letter refers to the use of a computer, numbers, and symbols

ASCIIS码: 1个英文字母(不分大小写)= 1个字节的空间

                    1个中文汉字 = 2个字节的空间

                    1个ASCII码 = 一个字节

UTF-8编码:1个英文字符 = 1个字节

                     英文标点  = 1个字节

                     1个中文 = 3个字节

                     中文标点 = 3个字节

Unicode编码:1个英文字符 = 2个字节

                     英文标点  = 2个字节

                     1个中文 = 2个字节

                     中文标点 = 2个字节
# 有错误的话,请指出

IO streams in Java

The Java IO stream comprising two input and output streams, each byte stream is divided into two categories, and character stream. The main classes are as follows

  1. File (File Characteristics and Management): information describing a file or directory, for example, generates a new directory, modify the file name, delete the file, determines the path files.

  2. The InputStream (Operation binary format): abstract class, based on the input operation byte is the parent class for all input streams. Defines the common feature of all input streams have.

  3. The OutputStream (Operation binary format): abstract class. Byte output operation based. It is the parent class of all output streams. It defines a common feature of all the output streams have.

4.Reader (Operation File Format): abstract class, a character based on the input operation.

  1. Writer (Operation File Format): abstract class, based on the operation of the output character.

  2. A RandomAccessFile (random file operations): a separate class, it inherits directly to the Object rich features, can be accessed (O) operated from anywhere in the file.

The concept and role stream

Stream: data source object data output capability or is capable of receiving the object data receiving end

Essence stream: data transmission, data transmission characteristics of the various types of flow abstract, convenient data manipulation more intuitive

InputStream中的基本方法

//读取一个字节数据,并返回读到的数据,如果返回-1,表示读到了输入流的末尾。
int read() 
//将数据读入一个字节数组,同时返回实际读取的字节数。如果返回-1,表示读到了输入流的末尾。
int read(byte[] b) 
//将数据读入一个字节数组,同时返回实际读取的字节数。如果返	 回-1,表示读到了输入流的末尾。off指定在数组b中存放数据的起始偏移位置;len指定读取的最大字节数。
int read(byte[]?b, int off, int len) 

End of stream determination: Method read () returns a value of -1; the readLine () returns the value is null.
OutputStream

void write(int b):往输出流中写入一个字节。
void write(byte[] b) :往输出流中写入数组b中的所有字节。
void write(byte[] b, int off, int len) :往输出流中写入数组b中从偏移量off开始的len个字节的数据。
void flush() :刷新输出流,强制缓冲区中的输出字节被写出。

Reader和Writer类似于上面

Converting character stream and byte stream

InputStreamReader: byte characters to bridge

OutputStreamWriter: byte characters to bridge

OutputStreamWriter (OutStreamout): The byte stream output character stream.

InputStreamReader (InputStream in): the character stream input to the byte stream.

区别

字节流没有缓冲区,是直接输出的,而字符流是输出到缓冲区的。因此在输出时,字节流不调用colse()方法时,信息已经输出了,而字符流只有在调用close()方法关闭缓冲区时,信息才输出。要想字符流在未关闭时输出信息,则需要手动调用flush()方法。

读写单位不同:字节流以字节(8bit)为单位,字符流以字符为单位,根据码表映射字符,一次可能读多个字节。

处理对象不同:字节流能处理所有类型的数据(如图片、avi等),而字符流只能处理字符类型的数据。

结论:只要是处理纯文本数据,就优先考虑使用字符流。除此之外都使用字节流。

原理:byte[]和String之间的互相转换

//在从字节流转化为字符流时:实际上就是byte[]转化为String时,
public String(byte bytes[], String charsetName)

//有一个关键的参数字符集编码,通常我们都省略了,那系统就用操作系统的lang

//而在字符流转化为字节流时,实际上是String转化为byte[]时,
byte[]   String.getBytes(String charsetName)
Released six original articles · won praise 0 · Views 835

Guess you like

Origin blog.csdn.net/csdn960616/article/details/92771477