The (Input / Output) stream in the IO system IO in Java is nothing more than, including character-based Stream, bytes-based Stream and Stream of Stream converted bytes-oriented Stream. (It is difficult to understand?) Byte-oriented Stream ---- InputStream / OutputStreaminputStream and OutputStream are two Abstact classes, and the two chicken ribs are expanded for bytes-oriented Stream (base class ^ _ ^ - INPUTSTREAM BYTEARRAYINPUTSTREAM - uses a buffer in memory as inputStream. Construct --- ByteArrayInputStream (Byte []) Creates a new byte array input stream that reads data from the specified byte array. --- ByteArrayInputStream (Byte [], int, int, creates a new byte array input stream that reads data from the specified byte array. --- Mark :: This byte array is not copied. StringBufferInputStream - put a String object as an InputStream. Note: StringBufferInputStream method is not recommended. Such can't convert characters correctly to bytes. Similar to the JDK version 1.1, the best way to create a stream from a string is to use the StringReader class. Construct --- StringBufferInputStream (String) The input stream of reading data is created according to the specified string. FILEINPUTSTREAM - Use a file as an InputStream to create a input file stream for the read operation of the file, and read data from the specified File object. --- FileInputStream (FileDescriptor) Creates a input file stream that reads data from the specified file descriptor. --- FileInputStream (String) Create an input file stream to read data from the file specified name. Method ---- read () reads one byte data from the current input stream. Read (Byte []) Reads the B.LENGTH data in the current input stream to a byte array. Read (Byte [], INT, INT) reads the LEN byte data in the input stream into a byte array. PipedInputStream: The concept of PIPE is implemented mainly in the thread. The pipe input stream refers to the receiving end of a communication pipe. A thread sends data through the pipe output stream, and the other thread reads data through the pipe input stream, so that communication between the two threads can be implemented. PipedInputStream () Creates a pipe input stream that has not been connected to a pipe output. PipedInputStream (PiredOutputStream) Creates a pipe input stream that is connected to a pipe output stream.
SequenceInputStream: Multiple InputStream is merged into an InputStream. "Sequence Enter" class allows the application to continuously merge several input streams and cause them like a single input stream. Each input stream is sequentially read until the end of the stream is reached. Then "Sequence Input" class closes this stream and automatically switches to the next input stream. SequenceInputStream (Enumeration) Creates a new sequence input stream and initializes it with the enumeration value of the specified input stream. SequenceInputStream (InputStream, InputStream) Creates a new sequence input stream, which is initialized to first read the input stream S1, and then read the input stream S2. --OutputSteam ByteArrayoutputStream: Store information in a buffer in memory. This class implements an output stream that is written in byte array. It automatically expands when the data is written to the buffer. Use TobyTearray () and toString () to retrieve data. Construct --- ByteArrayOutputStream () Creates a new byte array output stream. --- ByteArrayoutputStream () Creates a new byte array output stream. --- ByteArrayoutputStream (int) Create a new byte array output stream with a buffer capacity specified by the specified size byte. TOSTRING (String) converts the buffer content into a string according to the specified character encoding, and converts the byte into characters. Write (byte [], int, int) writes the LEN byte from the offset OFF in the specified byte array to the byte array output stream. Write (int) writes the specified byte to the byte array output stream. Writeto (OutputStream) uses Out.write (buf, 0, count) calling the write method of the output stream to write all the contents of the byte array output stream to the specified output stream parameters. FileOutputStream: File output stream is an output stream that outputs data to File or FileDescriptor. FileOutputStream (file) Creates a file output stream to output data to the specified File object. FileOutputStream (FileDescriptor) Creates a file output stream to output data to the specified file descriptor. FileOutputStream (String) creates a file output stream to output data to the file specified name. FileOutputStream (String, Boolean) Creates an output file with the specified system file name. PipedOutputStream: The pipe output stream refers to the transmitting end of a communication pipe. A thread sends data through the pipe output stream, and the other thread reads data through the pipe input stream, so that communication between the two threads can be implemented. PiredOutputStream () Creates a pipe output stream that has not been connected to a pipe input.
PiredOutputStream (PipedInputStream) Creates a pipe output stream that is connected to a pipe input stream. The character-oriented Stream Reader / Writer is a Unicode Character-oriented Stream indicates that the Unicode character is written from the stream from the stream or to the stream. Reader / Writer is the following types of STREAMs for ABSTACT classes, including Unicode characters, including:
- Reader
1) ChararrayReader: Create a CharaRrayReader with the ByTearRayInputStream Corresponding to CharaRrayReader (Char []) with the specified character array. ChararrayReader (CHAR [], int, int) creates a CharaRrayReader with a specified character array. 2) StringReader: Create a new string reader corresponding to StringReader (String) corresponding to StringBufferInputStream. 3) FileReader: corresponds to FileInputStream
4) PipedReader: corresponds to PiPedInputStream
- Writer
1) ChararrayWrite: Corresponds to byterrayoutputstream 2) StringWrite: Corresponding to byte-oriented Stream 3) FileWrite: Corresponds to FileOutputStream 4) PiPedWrite: Corresponds to PiPedoutputStream
Two unsatched StreamReams and OutputStreamReader: Convert a byte-oriented Stream into a character-oriented Stream. An InputStreamReader class is a bridge stream from byte stream: it reads into bytes, and converts it to a character stream according to the specified encoding method. The encoding method used may be specified by the name, or the platform acceptable default encoding method. One of the READ () methods of the InputStreamReader may cause one or more bytes from the basic byte input stream. In order to achieve greater efficiency, consider BufferedReader package InputStreamReader, BufferedReader in = new BufferedReader (new InputStreamReader (System.in)); InputStreamReader (InputStream) with the default character encoding, creates a InputStreamReader. InputStreamReader (InputStream, String) creates an InputStreamReader with named character encoding. OutputStreamWriter writes multiple characters to an output stream to convert multiple characters to bytes according to the specified character encoding. Each OutputStreamWriter merges its own Chartobyteconvert, so it is a bridge from the character stream to byte stream. FilterInputStream, RandomaccessFile see example. ObjectInputStream, ObjectOutputStream See additional blog.
General usage principles for Java IO:
First, according to the data source (destination) classification: 1, a file: FileInputStream, FileOutputStream, FileReader, FileWriter2, is a byte []: ByteArrayInputStream, ByteArrayOutputStream3, is Char []: CharArrayReader, CharArrayWriter4, is String: StringBufferInputStream, StringReader, StringWriter5, Network data stream: InputStream, OutputStream, Reader, Writer 2, pressing whether to format the output: 1, to format the output: PrintStream, PrintWriter
Third, press whether you want to slow down: 1, bufferedinputstream, bufferedoutputstream, bufferedreader, bufferedwriter
Fourth, according to data format: 1, binary format (as long as it is plain text): InputStream, OutputStream and all subclats 2, pure text format (including pure English and Chinese characters or other coding methods); Reader, Writer and all its subclasses with Reader, Writer
5. Press the input and output points: 1. Enter: Reader, InputStream type subclass 2, output: Writer, OutputStream type subclass
Six special needs: 1, from a Stream to Reader, Writer conversion classes: InputStreamReader, OutputStreamWriter2, the object O: ObjectInputStream, ObjectOutputStream3, inter-process communication: PipeInputStream, PipeOutputStream, PipeReader, PipeWriter4, combined input: SequenceInputStream5, more specific needs : PushbackInputStream, PushbackReader, LinenumberinputStream, LinenumberReader
The general guidelines for determining which class and its constructing process are as follows (do not consider special needs): First, consider what is the most original data format: Principle four second, is the input or output: Principle Five, whether it is necessary to convert the stream : Principles six first points fourth, data source (go) What is: Principles a fifth, whether it is going to buffer: principle three (specifically: must pay attention to whether readline () is defined, what is more than READ, WRITE is more special input or output method) Sixth, is it necessary to format the output: Principle II