The basic function of IO stream: file byte stream, file character stream, buffer byte stream, buffer character stream byte array stream, data stream, conversion stream, object stream

The basic function of IO stream:
Insert picture description here

1. File byte stream: FileInputStream, FileOutputStream, the node stream that directly links the file, the function is to process the binary file, and read and output the file through the byte type variable or array.
Use FileInputStream to read file content

  1. abstract int read( );
  2. int read( byte b[ ] );
  3. int read( byte b[ ], int off, int len );
  4. int available( );
  5. close( );
    Use FileOutputStream to write content to file
  6. abstract void write( int b );
  7. void write( byte b[ ] );
  8. void write( byte b[ ], int off, int len );
  9. void flush( );
  10. void close( );
    2. File character stream: Reader, Writer, are also node streams used to read and write files, the data source is a file, and the character type array or variable is used as a transfer station to operate, often used for operation Plain text file.
    Use Reader to read file content
  11. int read( );
  12. int read( char [ ]cbuf );
  13. int read( char [ ]cbuf, int off, int len );
  14. int available( );
  15. close( );
    Use Writer to write content to file
  16. void write( int c );
  17. void write( char[]cbuf);
  18. abstract void write( char [ ]cbuf, int off, int len );
  19. void write(String str);
  20. abstract void flush( );
  21. void close( );
    3. Buffered byte stream: BufferedInputStream, BufferedOutputStream, is a processing stream used to improve the efficiency of reading and writing. Buffered byte stream has a byte between the transfer station and the data source and the transfer station and the target The type of array is used to temporarily store bytes. The size of this array is 8192. This processing improves efficiency. BufferedInputStream and BufferedOutputStream are processing streams (packaging streams).
  22. Both reading and writing files use buffers, which reduces the number of reads and writes, thereby improving efficiency. 2) When creating the objects of these two buffer streams, an internal buffer array is created, and a 32-byte buffer is used by default. Area.
  23. When reading data, the data is read into the buffer in blocks, and subsequent read operations directly access the buffer
  24. When writing data, the buffer is first written, and when the buffer is full, the data is written to the connected output stream. Use the method flush() to force the contents of the buffer to be written to the output stream
  25. The order of closing the flow is opposite to the order of opening the flow. Just close the high-level flow, and close the low-level node flow that the high-level flow actually closed
  26. Use of Flush: Manually write the contents of the buffer to the file
    . 4. Buffered character stream byte array stream: BufferedReader readLine() reads the data of a text line BufferedWriter newLine(); writes a line separator. Using a buffered character stream is a common way to copy text files.
    5. Data flow: DataInputStream and DataOutputStream
  27. Provides access to all Java basic types of data (such as: int, double, etc.) and String methods.
  28. Processing stream, only for byte stream, binary file
  29. Input flow chain and output flow chain
  30. Note: As long as you close the upper stream
    . 6. Convert the stream: InputStreamReader and OutputStreamWriter process streams are used to convert byte streams into character streams. The role of the bridge between character stream and byte stream InputStreamReader is to convert InputStream into Reader. Converting OutputStream to Writer There is a conversion stream that converts a byte stream into a character stream, because the character stream operation text is simpler, there is no conversion stream that converts a character stream into a byte stream, because there is no need for System.in to represent standard input, that is Keyboard input is an instance of InputStream.
    7. Object Stream: Object Serialization (Serialization) ObjectOutputStream, serialization  write object, write the object to (file) in the form of "binary/byte". ObjectInputStream (deserialization, read object), converts Java objects into byte sequences (IO byte stream) Object Deserialization (DeSerialization) restores Java objects from byte sequences.

Guess you like

Origin blog.csdn.net/weixin_44192389/article/details/100985738