Data stream - Three-flow principle

Data Flow Description:

Introduction:

The flow of data, referred to as data exchange between streams, hard disk and memory

Flow in the direction of sub-divided into the input and output streams, as a reference to memory, the data source is read into memory, an input stream, also referred to read the stream. The write data from the source memory, the output stream, also known as write stream.

Exiled the I / O package

Data stream classification

--- into a byte stream, a stream of characters and objects flow.

  1. Byte stream: in byte transmission, can transmit all types
  2. Character stream: you can only transmit a string
  3. Object flow: transfer object (JSON)
    computer binary. The transmission data are transmitted in units of bytes. Therefore, regardless of the kind of stream, its essence is the byte stream. However, in order to facilitate the transmission of character data and object data, the byte stream may be carried out on a package basis, and form a character stream object streams, all streams are used to receive an int;
    the InputStream and OutputStrem:
    the InputStream stream of bytes and OutpuuStrem two parent class, the class is abstract two, a read () and write () two abstract methods; made by subclasses to achieve the characteristics of their respective data sources

Procedure stream

  1. Establish flow

  2. Operation Flow

  3. Close flow
    when operating as a file stream, the stream will throw to read the file Q. find FileNotFoundException exception. New documents will be written to the stream, but only if the directory where the file must exist. Otherwise it will throw FileNotFoundException
    create read stream:
    temporary files -> Memory -> IO streams

    Character stream:

    import io     # 导入io模块
    sio = io.StringIO()   # 创建一个对象,进行保存读取
    
    sio.write("hello")    # 写入
    print(sio.getvalue())     # 读取  hello
    
    sio.close()    # close之后内容就没有了

    Byte stream (str + list bytes + bytearray):

    import io
    bio = io.BytesIO()
    bio.write(b"hello")        # 写入
    print(bio.getvalue())      # 读取  b'hello'
    bio.close()      # close

    sys module (stream operation):

    import sys
    
    # 标准输出流
    sys.stdout.write('123\n')
    
    # 标准输入流
     res = sys.stdin.readline()
     print(res)
    
    # 标准错误流
    sys.stderr.write('opq\n')
    
    三种流 :  不同种流异步执行!!!
          同种流有执行顺序(争夺cpu 资源)

Guess you like

Origin www.cnblogs.com/mqhpy/p/12104581.html