Java: Byte stream and character stream (input stream and output stream)

Source: Internet
Author: User

The content of this article:
    • What is a stream
    • BYTE stream
    • Character Stream

Starting Date: 2018-07-24

what is a stream

    • Flow is an abstract concept, is an abstraction of input, the input stream can be regarded as an input channel, the output stream can be regarded as an output channel.
    • The input stream is relative to the program, and the external incoming data to the program requires an input stream.
    • The output stream is relative to the program, and the program transfers the data to the outside with the help of the output stream.

byte stream--the most basic unit of transmitted data during transmission is the flow of bytes.

character stream-the most basic unit of transmitted data during transmission is the stream of characters.

 

But when we read the data, if we need to "look at the data", then the byte stream data needs to specify the character encoding so that we can see the characters we recognize, and the character stream, Because the character encoding method has been chosen, it is usually not necessary to change it (unless the defined character encoding is inconsistent with the original encoding method of the data!).

Byte stream

    • The class of a byte stream usually ends with a stream

byte input stream:

The most commonly used byte input streams are:

  • InputStream
  • FileInputStream
  • Bufferedinputstream "Bufferedinputstream is not a direct implementation subclass of InputStream, it is a subclass of FilterInputStream"their difference with the use:
    • InputStream is the abstract base class for a byte input stream, and InputStream as a base class that defines several common functions for its base class:
        • Read (byte[] b): Read B's length of bytes of data from the stream to B, return the result is the number of bytes read (when re-read, if the return 1 description to the end, no data)
        • read(byte[] b, int off, int len):从流中从off的位置开始读取len个字节的数据存储到b中,返回结果是实际读取到的字节个数(当再次读时,如果返回-1说明到了结尾,没有了数据)
        • Close (): Closes the stream and frees the resource.
    • FileInputStream is primarily used to manipulate the file input stream, which implements the read () function of the base class (without parameters), in addition to the functions defined by the base class:
        • Read (): Reads 1 bytes of data from the stream, returns the result as an int, (if the encoding is one character in one byte, you can try to turn to char to view the data).
    • Bufferedinputstream with the meaning of buffering, the normal reading is read from the hard disk, and with the buffer, Bufferedinputstream has been in advance to encapsulate the data into memory, in-memory operation of the data quickly, so its efficiency should be non-buffered high. In addition to the functions defined by the base class, it implements the Read () function of the base class (with no parameters):
        • Read (): Reads 1 bytes of data from the stream, returns the result as an int, (if the encoding is one character in one byte, you can try to turn to char to view the data).
    use:
      • InputStream is an abstract base class, so it is not possible to create objects, but it can be used to "interface programming" because most of the subclass's function base classes are defined, so use the base class to invoke the function.
      • FileInputStream is a stream used to read file data, so it requires a file object to instantiate, which can be either a file object or a file name path string. "The file doesn't exist here, it's wrong."

      • Bufferedinputstream is a flow that encapsulates other flows to improve efficiency, so its initialization requires a InputStream stream object.

    byte output stream:

    The most commonly used byte output streams are:

      • OutputStream
      • FileOutputStream
      • Bufferedoutputstream "Bufferedoutputstream is not a direct implementation subclass of OutputStream, it is a subclass of Filteroutputstream"
    their difference with the use:
    • OutputStream is the base class for the byte output stream, and OutputStream as the base class, which defines several common functions for its base class:
        • Write (byte[] b): Writes the length byte data of B to the output stream.
        • Write (byte[] b,int off,int len): Starts with the off position of B, fetches the Len byte data and writes it to the output stream.
        • Flush (): refreshes the output stream and writes the data to the output stream immediately.
        • Close (): Closes the stream and frees the system resources.
    • FileOutputStream is an output stream for writing files, which, in addition to the functions defined by the base class, implements the OutputStream abstract function, write (int b):
        • Write (int b): Turns B into a byte of data, written to the output stream.
    • Bufferedoutputstream, like the bufferedinputstream above, can improve efficiency. In addition to the functions defined by the base class, it implements the abstract function of OutputStream, write (int b):
        • Write (int b): Turns B into a byte of data, written to the output stream.
    use:
      • OutputStream is an abstract base class, so it cannot be instantiated, but it can be used for interface programming.
      • FileOutputStream is the output stream used to write the file, so it needs a file as an instantiation parameter, which can be a file object or a path string. "If the file does not exist, it will be created automatically. "" FileOutputStream can give the second argument when instantiated, the second parameter is whether to use append write default, True when the original file content is appended to write data, the default is false "

      • Bufferedoutputstream requires an output stream as an instantiated parameter.

    Add:
      • Some of the above functions, given the efficiency problem, may override the function of the base class, but the function is essentially the same.
      • More about the function and usage of the byte stream can refer to the JDK documentation.

    character Stream

      • Classes of character streams usually end in reader and writer

    character input stream:

    The common character input streams are:

      • Reader
      • InputStreamReader
      • FileReader
      • BufferedReader
    their difference with the use:
    • Reader is the abstract base class for the character input stream, which defines the following functions:
        • Read (): reads a single character, returns the result of an int, needs to be converted to char, and returns 1 when the end of the stream is reached
        • Read (char[] cbuf): reads cbuf length characters to Cbuf, returns the number of characters read, returns 1 when the end of the stream is reached
        • Close (): Closes the stream, releasing the system resources that are occupied.
    • InputStreamReader can convert byte data streams in InputStream to character data streams based on character encoding. In addition to the functions defined by the base class, it implements the following functions itself:
      • Read (char[] cbuf, int offset, int length): Starts at offset position, reads the length character to Cbuf, returns the number of characters actually read, and returns 1 when the end of the stream is reached.
    • FileReader can convert byte data in FileInputStream into character data streams based on character encoding.
    • BufferedReader can encapsulate the character input stream and buffer the data to improve the reading efficiency. In addition to the functions defined by the base class, it implements the following functions itself:
      • Read (char[] cbuf, int offset, int length): Starts at offset position, reads the length character to Cbuf, returns the number of characters actually read, and returns 1 when the end of the stream is reached.
      • ReadLine (): reads a line of text, ends with a line terminator, and returns the string that the result is read. Returns null if the end of the stream has been reached

    Use
      • Reader is an abstract base class that cannot be instantiated, but can be used for interface programming.
      • InputStreamReader requires a byte input stream object as an instantiated parameter. You can also specify the second parameter, the second parameter is the character encoding, either as a string of encodings or as a character set object.

      • FileReader requires a file object as an instantiated parameter, either as a file class object, or as a path string for files.

      • Bufferreader requires a character input stream object as an instantiated parameter.

    character output stream:

    The common character output streams are:

      • Writer
      • OutputStreamWriter
      • FileWriter
      • BufferedWriter
    their difference with the use:
    • Writer is the abstract base class for the character output stream, which defines the following functions
        • Write (char[] cbuf): Writes an array of characters toward the output stream.
        • Write (int c): Writes a character toward the output stream.
        • Write (String str): Writes a string of strings toward the output stream.
        • Write (string str, int off, int len): writes a portion of the string to the output stream.
        • Close (): Closes the stream and frees the resource. "This is still abstract, written out to show that there is this closing function."
        • flush():刷新输出流,把数据马上写到输出流中。 【这个还是抽象的,写出来是说明有这个关闭功能】
    • OutputStreamWriter can allow us to write string data directly into the stream, which will help us to convert character data into byte data according to character encoding and then write to the output stream, which is equivalent to a mediation bridge.
    • FileWriter is similar to the OutputStreamWriter function, we can write string data directly into the stream, and the FileWriter internally will convert character data into byte data and write to the output stream based on the character encoding.
    • BufferedWriter is a bit more advanced than FileWriter, which uses buffers to improve write efficiency. It also has one more function:
      • NewLine (): Writes a line break.

    Use
      • Writer is an abstract base class that cannot be instantiated, but can be used for interface programming.
      • OutputStreamWriter requires an input stream object as an instantiated parameter.

      • FileWriter requires a file object to instantiate, which can be either a file class object or a path string for the files.

      • Bufferwriter

    Add:
      • Some of the above functions, given the efficiency problem, may override the function of the base class, but the function is essentially the same.
      • You can refer to the JDK documentation for more functions and usage of character streams.

Java: Byte stream and character stream (input stream and output stream)

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.