Reprinted: Java: byte stream and character stream (input and output streams)

 


In this article:

  • What is the flow
  • Byte stream
  • Jifuryu

 

Starting date: 2018-07-24

 


What is the flow

 

  • Stream is an abstract concept, abstract input and output device, the input stream can be considered an input channel, the output stream can be considered one output channel.
  • In terms of the input stream is relatively program external to the program needs the incoming data stream input.
  • Contrasting with the output stream of the program, the program needs the data transferred to the external output stream.

 

What is a byte stream?

Byte stream - the transmission process, the transmission data is a basic unit of a stream of bytes.

What is the character stream?

Character stream - the transmission process, the basic unit of data transmission is a stream of characters.

 

Different character encoding, sometimes a number of bytes used by the characters are not the same, such as ASCLL encoded characters, representing a byte; and UTF-8 encoded characters, an English character requires one byte, a Chinese needs three bytes.

Byte data is in binary form, we can identify to turn into a normal character, you need to select the correct encoding. Garbled question encountered in our lives is to choose the correct byte data is not coding to appear as characters.

In essence, when the write data (i.e., output), Ye byte, character or, identifiers are essentially no need to specify the encoding.

However, when reading data, if we need to "look at the data," then the byte stream of data need to specify the character encoding, so that we can see the character we can recognize; and character stream, as it has been selected your character encoding usually do not need to change the (unless the original character encoding data encoding defined inconsistent!)

In the transmission, since the nature of the computer are transmitted byte, and a character composed of a plurality of bytes, first look-up table to turn into a byte into bytes prior to transfection, so that the transmission buffer is sometimes used.

 

 


Byte stream

 

  • Class byte stream stream typically ends

 

Input stream of bytes:

The input stream of bytes used are:

  • InputStream  
  • FileInputStream
  • BufferedInputStream [BufferedInputStream not direct implementation of InputStream subclass is a subclass of FilterInputStream]

    The difference between them and uses:

    • InputStream abstract base class is the input byte stream, as a base InputStream class to its base class defines several common functions:
        • read (byte [] b): the length of b bytes of read data stored to the stream from (b), the result is the number of bytes read (when called again, it will return -1 to the end of the description, no data)
        • read(byte[] b, int off, int len):从流中从off的位置开始读取len个字节的数据存储到b中,返回结果是实际读取到的字节个数(当再次读时,如果返回-1说明到了结尾,没有了数据)
        • close (): Closes the stream, free up resources.
    • FileInputStream file input stream is mainly used to operate, it may be used in addition to the functions defined in the base class, it implements the read () function (with no arguments) base class:
        • read (): read a byte from the stream data, the result is an int, (if the coding is a one byte character, try to turn char, to view the data).
    • Buffered BufferedInputStream mean, normal read is read from the hard disk inside, and then with a buffer, the data is encapsulated BufferedInputStream in advance into memory, manipulating data in memory to be fast, so its efficiency is to be unbuffered high. It may be used in addition to the definition of the base class, the base class also implements the read () function (no arguments):
        • read (): read a byte from the stream data, the result is an int, (if the coding is a one byte character, try to turn char, to view the data).

    use:

    • InputStream is an abstract base class, so it can not create an object, but it can be used to "Programming Interface", because most of the base class has subclasses defined, so the use of the base class to call the function.
    • FileInputStream file is used to read the data stream, so it needs a file object used to instantiate, this may be a File object file, the file name can also be a path string. File does not exist here [wrong] will throw

    image

    • A package BufferedInputStream is another flow stream to enhance the efficiency, it is initialized with a stream of InputStream objects.

    image

     

    Output stream of bytes:

    The output stream of bytes used are:

    • OutputStream
    • FileOutputStream
    • BufferedOutputStream [BufferedOutputStream not a direct implementation of OutputStream subclass is a subclass of FilterOutputStream]

    The difference between them and uses:

    • Is the base class OutputStream output stream of bytes, as the base class OutputStream, to its base class defines several common functions:
        • write (byte [] b): the length of b bytes of data written to the output stream.
        • write (byte [] b, int off, int len): starting from the off position b, len bytes of data acquisition, written to the output stream.
        • flush (): refresh the output stream, the data is immediately written to the output stream.
        • close (): Closes the stream, free system resources.
    • FileOutputStream is an output stream for writing a file, it may be used in addition to the base class definition, but also to achieve the abstract function write (int b) OutputStream of:
        • write (int b): The b turn into a byte of data written to the output stream.
    • BufferedOutputStream the same as above that BufferedInputStream, can improve efficiency. In addition to its function in the base class definition may be used, but it implements abstract function write (int b) OutputStream of:
        • write (int b): The b turn into a byte of data written to the output stream.

    use:

    • OutputStream is an abstract base class, so it can not be instantiated, but it can be an interface for programming.
    • FileOutputStream is an output stream for writing a file, it requires a file as an example parameter, this may be a File object file, the file may be a path string. [If the file does not exist, it is created automatically. [] Can give the second parameter, the second parameter is used when FileOutputStream whether additional writing is instantiated by default, represents the original file content later additional writing of data is true, the default is false]

    image

    • BufferedOutputStream requires an output stream as the instantiation parameters.

    image

     

    supplement:

    • Some of the above functions, taking into account the efficiency of the above subclasses may override of the base class, but the function is basically unchanged.
    • More about the byte stream function and usage can refer to jdk documentation.

     

     


    Jifuryu

     

    • Class character stream reader and writer ending generally

     

     

    Character-input stream:

    Common character input stream are:

    • Reader
    • InputStreamReader
    • FileReader
    • BufferedReader

    The difference between them and uses:

    • Reader input character stream abstract base class, which defines several of the following functions:
        • read (): read a single character, the result is an int, char required to turn; end of the stream is reached, -1
        • read(char[] cbuf):读取cbuf的长度个字符到cbuf这种,返回结果是读取的字符数,到达流的末尾时,返回-1
        • close()  :关闭流,释放占用的系统资源。
    • InputStreamReader 可以把InputStream中的字节数据流根据字符编码方式转成字符数据流。它除了可以使用基类定义的函数,它自己还实现了以下函数:
      • read(char[] cbuf, int offset, int length) :从offset位置开始,读取length个字符到cbuf中,返回结果是实际读取的字符数,到达流的末尾时,返回-1
    • FileReader 可以把FileInputStream中的字节数据转成根据字符编码方式转成字符数据流。
    • BufferedReader可以把字符输入流进行封装,将数据进行缓冲,提高读取效率。它除了可以使用基类定义的函数,它自己还实现了以下函数:
      • read(char[] cbuf, int offset, int length) :从offset位置开始,读取length个字符到cbuf中,返回结果是实际读取的字符数,到达流的末尾时,返回-1
      • readLine() :读取一个文本行,以行结束符作为末尾,返回结果是读取的字符串。如果已到达流末尾,则返回 null

     

     

    使用

    • Reader 是一个抽象基类,不能实例化,但可以用于接口化编程。
    • InputStreamReader需要一个字节输入流对象作为实例化参数。还可以指定第二个参数,第二个参数是字符编码方式,可以是编码方式的字符串形式,也可以是一个字符集对象。

    image

     

    • FileReader 需要一个文件对象作为实例化参数,可以是File类对象,也可以是文件的路径字符串。

    image

     

    • BufferReader需要一个字符输入流对象作为实例化参数。

    image

    image

     

    字符输出流:

    常见的字符输出流有:

    • Writer
    • OutputStreamWriter
    • FileWriter
    • BufferedWriter

    他们的区别与用途:

    • Writer是字符输出流的抽象基类, ,它定义了以下几个函数
      • write(char[] cbuf) :往输出流写入一个字符数组。
      • write(int c) :往输出流写入一个字符。
      • write(String str) :往输出流写入一串字符串。
      • write(String str, int off, int len) :往输出流写入字符串的一部分。
      • close() :关闭流,释放资源。 【这个还是抽象的,写出来是说明有这个关闭功能】
      • flush():刷新输出流,把数据马上写到输出流中。 【这个还是抽象的,写出来是说明有这个关闭功能】
    • OutputStreamWriter可以使我们直接往流中写字符串数据,它里面会帮我们根据字符编码方式来把字符数据转成字节数据再写给输出流,它相当于一个中介\桥梁。
    • FileWriter与OutputStreamWriter功能类似,我们可以直接往流中写字符串数据,FileWriter内部会根据字符编码方式来把字符数据转成字节数据再写给输出流。
    • BufferedWriter比FileWriter还高级一点,它利用了缓冲区来提高写的效率。它还多出了一个函数:
      • newLine() :写入一个换行符。

     

    使用

    • Writer 是一个抽象基类,不能实例化,但可以用于接口化编程。
    • OutputStreamWriter 需要一个输入流对象作为实例化参数。

     image

    • FileWriter 需要一个文件对象来实例化,可以是File类对象,也可以是文件的路径字符串。

     image

    • BufferWriter

    image

     image

     

    补充:

    • 上面的一些函数,考虑到效率问题,上面的子类可能会重写基类的函数,但功能基本是不变的。
    • 更多关于字符流的函数与用法可以参考jdk文档。
    • 转载来源:https://www.cnblogs.com/progor/p/9357676.html

     

     


 


本文内容:

  • 什么是流
  • 字节流
  • 字符流

 

首发日期:2018-07-24

 


什么是流

 

  • 流是个抽象的概念,是对输入输出设备的抽象,输入流可以看作一个输入通道,输出流可以看作一个输出通道。
  • 输入流是相对程序而言的,外部传入数据给程序需要借助输入流。
  • 输出流是相对程序而言的,程序把数据传输到外部需要借助输出流。

 

什么是字节流?

字节流--传输过程中,传输数据的最基本单位是字节的流。

什么是字符流?

字符流--传输过程中,传输数据的最基本单位是字符的流。

 

字符编码方式不同,有时候一个字符使用的字节数也不一样,比如ASCLL方式编码的字符,占一个字节;而UTF-8方式编码的字符,一个英文字符需要一个字节,一个中文需要三个字节。

字节数据是二进制形式的,要转成我们能识别的正常字符,需要选择正确的编码方式。我们生活中遇到的乱码问题就是字节数据没有选择正确的编码方式来显示成字符。

从本质上来讲,写数据(即输出)的时候,字节也好,字符也好,本质上都是没有标识符的,需要去指定编码方式。

但读数据的时候,如果我们需要去“看数据”,那么字节流的数据需要指定字符编码方式,这样我们才能看到我们能识别的字符;而字符流,因为已经选择好了字符编码方式,通常不需要再改了(除非定义的字符编码方式与数据原有的编码方式不一致!)

在传输方面上,由于计算机的传输本质都是字节,而一个字符由多个字节组成,转成字节之前先要去查表转成字节,所以传输时有时候会使用缓冲区。

 

 


字节流

 

  • 字节流的类通常以stream结尾

 

字节输入流:

常用的字节输入流主要有:

  • InputStream  
  • FileInputStream
  • BufferedInputStream 【BufferedInputStream不是InputStream的直接实现子类,是FilterInputStream的子类】

    他们的区别与用途:

    • InputStream是字节输入流的抽象基类 ,InputStream作为基类,给它的基类定义了几个通用的函数:
        • read(byte[] b):从流中读取b的长度个字节的数据存储到b中,返回结果是读取的字节个数(当再次读时,如果返回-1说明到了结尾,没有了数据)
        • read(byte[] b, int off, int len):从流中从off的位置开始读取len个字节的数据存储到b中,返回结果是实际读取到的字节个数(当再次读时,如果返回-1说明到了结尾,没有了数据)
        • close():关闭流,释放资源。
    • FileInputStream主要用来操作文件输入流,它除了可以使用基类定义的函数外,它还实现了基类的read()函数(无参的):
        • read():从流中读取1个字节的数据,返回结果是一个int,(如果编码是以一个字节一个字符的,可以尝试转成char,用来查看数据)。
    • BufferedInputStream带有缓冲的意思,普通的读是从硬盘里面读,而带有缓冲区之后,BufferedInputStream已经提前将数据封装到内存中,内存中操作数据要快,所以它的效率要要非缓冲的要高。它除了可以使用基类定义的函数外,它还实现了基类的read()函数(无参的):
        • read():从流中读取1个字节的数据,返回结果是一个int,(如果编码是以一个字节一个字符的,可以尝试转成char,用来查看数据)。

    使用:

    • InputStream是抽象基类,所以它不可以创建对象,但它可以用来“接口化编程”,因为大部分子类的函数基类都有定义,所以利用基类来调用函数。
    • FileInputStream是用来读文件数据的流,所以它需要一个文件对象用来实例化,这个文件可以是一个File对象,也可以是文件名路径字符串.【这里文件不存在会抛错】

    image

    • BufferedInputStream是一种封装别的流以提高效率的流,所以它的初始化需要一个的InputStream流对象。

    image

     

    字节输出流:

    常用的字节输出流主要有:

    • OutputStream
    • FileOutputStream
    • BufferedOutputStream 【BufferedOutputStream不是OutputStream的直接实现子类,是FilterOutputStream的子类】

    他们的区别与用途:

    • OutputStream是字节输出流的基类, OutputStream作为基类,给它的基类定义了几个通用的函数:
        • write(byte[] b):将b的长度个字节数据写到输出流中。
        • write(byte[] b,int off,int len):从b的off位置开始,获取len个字节数据,写到输出流中。
        • flush():刷新输出流,把数据马上写到输出流中。
        • close():关闭流,释放系统资源。
    • FileOutputStream是用于写文件的输出流,它除了可以使用基类定义的函数外,还实现了OutputStream的抽象函数write(int b):
        • write(int b):将b转成一个字节数据,写到输出流中。
    • BufferedOutputStream像上面那个BufferedInputStream一样,都可以提高效率。它除了可以使用基类定义的函数外,它还实现了OutputStream的抽象函数write(int b):
        • write(int b):将b转成一个字节数据,写到输出流中。

    使用:

    • OutputStream是抽象基类,所以它不能实例化,但它可以用于接口化编程。
    • FileOutputStream是用于写文件的输出流,所以它需要一个文件作为实例化参数,这个文件可以是File对象,也可以是文件路径字符串。【如果文件不存在,那么将自动创建。】【FileOutputStream实例化时可以给第二个参数,第二个参数是是否使用追加写入默认,为true时代表在原有文件内容后面追加写入数据,默认为false】

    image

    • BufferedOutputStream需要一个输出流作为实例化参数。

    image

     

    补充:

    • 上面的一些函数,考虑到效率问题,上面的子类可能会重写基类的函数,但功能基本是不变的。
    • 更多关于字节流的函数与用法可以参考jdk文档。

     

     


    字符流

     

    • 字符流的类通常以reader和writer结尾

     

     

    字符输入流:

    常见的字符输入流有:

    • Reader
    • InputStreamReader
    • FileReader
    • BufferedReader

    他们的区别与用途:

    • Reader是字符输入流的抽象基类 ,它定义了以下几个函数:
        • read() :读取单个字符,返回结果是一个int,需要转成char;到达流的末尾时,返回-1
        • read(char[] cbuf):读取cbuf的长度个字符到cbuf这种,返回结果是读取的字符数,到达流的末尾时,返回-1
        • close()  :关闭流,释放占用的系统资源。
    • InputStreamReader 可以把InputStream中的字节数据流根据字符编码方式转成字符数据流。它除了可以使用基类定义的函数,它自己还实现了以下函数:
      • read(char[] cbuf, int offset, int length) :从offset位置开始,读取length个字符到cbuf中,返回结果是实际读取的字符数,到达流的末尾时,返回-1
    • FileReader 可以把FileInputStream中的字节数据转成根据字符编码方式转成字符数据流。
    • BufferedReader可以把字符输入流进行封装,将数据进行缓冲,提高读取效率。它除了可以使用基类定义的函数,它自己还实现了以下函数:
      • read(char[] cbuf, int offset, int length) :从offset位置开始,读取length个字符到cbuf中,返回结果是实际读取的字符数,到达流的末尾时,返回-1
      • readLine() :读取一个文本行,以行结束符作为末尾,返回结果是读取的字符串。如果已到达流末尾,则返回 null

     

     

    使用

    • Reader 是一个抽象基类,不能实例化,但可以用于接口化编程。
    • InputStreamReader需要一个字节输入流对象作为实例化参数。还可以指定第二个参数,第二个参数是字符编码方式,可以是编码方式的字符串形式,也可以是一个字符集对象。

    image

     

    • FileReader 需要一个文件对象作为实例化参数,可以是File类对象,也可以是文件的路径字符串。

    image

     

    • BufferReader需要一个字符输入流对象作为实例化参数。

    image

    image

     

    字符输出流:

    常见的字符输出流有:

    • Writer
    • OutputStreamWriter
    • FileWriter
    • BufferedWriter

    他们的区别与用途:

    • Writer是字符输出流的抽象基类, ,它定义了以下几个函数
      • write(char[] cbuf) :往输出流写入一个字符数组。
      • write(int c) :往输出流写入一个字符。
      • write (String str): To a series of strings written to the output stream.
      • write (String str, int off, int len): To a portion of the string is written to the output stream.
      • close (): Closes the stream, free up resources. [This is abstract, there is this description is written off function]
      • flush():刷新输出流,把数据马上写到输出流中。 【这个还是抽象的,写出来是说明有这个关闭功能】
    • OutputStreamWriter allows us to write string data directly to the stream, which it will help us to put data into a character according to the character encoding of data bytes and then written to the output stream, which is equivalent to an intermediary \ bridge.
    • FileWriter OutputStreamWriter with similar functions, we can write directly to the string data stream, will be the inner FileWriter character data into byte data addressed to another character encoding in accordance with the output stream.
    • BufferedWriter than FileWriter also advanced a little, which uses the write buffer to improve efficiency. It is also more of a function:
      • newLine (): Writes a newline character.

     

    use

    • Writer is an abstract base class that can not be instantiated, but may be an interface for programming.
    • OutputStreamWriter require an input stream object as the instantiation parameters.

     image

    • FileWriter need to instantiate a file object, the object class may be a File, the path string may be a file.

     image

    • BufferWriter

    image

     image

     

    supplement:

    • Some of the above functions, taking into account the efficiency of the above subclasses may override of the base class, but the function is basically unchanged.
    • More about character stream function and usage can refer to jdk documentation.
    • Reprinted Source: https://www.cnblogs.com/progor/p/9357676.html

     

     


Guess you like

Origin www.cnblogs.com/yanht/p/11652429.html