I/O mainstream, 16-bit and 8-bit
16-bit writer and Reader
Based on different programming purposes
Filewriter and filewriter read and write files.
Stringwriter and stringreader read and write the locations in the memory.
Pipedwrier and pipedreader communicate with each other using eight bits for inputstream and outputstream.
Based on different programming purposes
Fileinputstream and fileoutputstream read and write files.
Bytearrayinputstream and bytearrayoutputstream read and write the byte array
Pipedinputstream and pipedoutputstream are used for inter-thread communication.
Note: There is no string Io operation in the data stream, so we need to use the bridge between reader and writer in inputstreamreader, outputstreamwriterbufferedwriter, bufferedreader, bufferedinputstream, bufferedoutputstream to increase the IO speed, note that it is best to package buffered first during packaging, which will improve the effect. Understanding of packaging mode. Memory of the length of various data types in package pattern.
Pay attention to the use of object streams.
Advanced Io operations:
Channel Io, java. NiO. Channels package
Its main feature is its ability to perform fast read/write operations on memory and perform memory ing.
At the same time, various data types of buffers can be created to improve Io operation efficiency.
Java stream processing is divided into two types: byte stream and word throttling. The byte stream processing unit is two bytes of Unicode characters, operating characters, character arrays or strings respectively, while the byte stream processing unit is one byte, operating byte and byte array.
In Java, Unicode encoding is used to store characters. The character stream processing class is used to convert external encoded character streams and Java Unicode Character streams. Inputstreamreader and outputstreamwriter handle the conversion between two streams and the word throttling. Merge streams (one buffer can be processed at a time) One operation is more efficient than word throttling (one byte at a time.
(1) byte-oriented stream ------ inputstream/outputstream
Inputstream and outputstream are two abstact classes. For byte-oriented stream, these two chicken ribs are extended (base class pai_^); 1. inputstream 1.1 bytearrayinputstream -- use a buffer in the memory as inputstream. construct --- (a) bytearrayinputstream (byte []) Create a New byte array input stream (bytearrayinputstream), which reads data from the specified byte array (using Byte as its buffer array) (B) --- bytearrayinputstream (byte [], Int, INT) creates a new byte array input stream, which reads data from the specified byte array. --- Mark: the byte array is not copied. 1.2 stringbufferinputstream -- use a String object as an inputstream. Construct --- stringbufferinputstream (string) to create an input stream string for reading data. Note: The stringbufferinputstream method is not recommended. This class cannot correctly convert characters into bytes. Similar to JDK 1.1, the best way to create a stream from a string is to use the stringreader class. 1.3 fileinputstream -- use a file as the inputstream to read the file construct --- (a) fileinputstream (File Name) to create an input file stream and read data from the specified file object. (B) fileinputstream (filedescriptor) creates an input file stream to read data from the specified file descriptor. (C)-fileinputstream (string name) creates an input file stream to read data from a file with the specified name. Method ---- read () reads one byte of data from the current input stream. Read (byte []) reads B. length bytes of data in the current input stream to a byte array. Read (byte [], Int, INT) reads len bytes of data in the input stream into a byte array. 1.4 pipedinputstream: implements the pipe concept. It is mainly used in the process. The input stream in the pipeline refers to the receiving end of a communication pipeline. One thread sends data through the output stream of the pipeline, while the other thread reads data through the input stream of the pipeline, which enables communication between the two threads. Construct --- pipedinputstream () creates an input stream of the pipeline, which is not connected to an output stream of the pipeline. Pipedinputstream (pipedoutputstream) creates a pipeline input stream, which is connected to a pipeline output stream. 1.5 sequenceinputstream: Merge multiple inputstreams into one inputstream. The "sequential input stream" class allows applications to combine several input streams consecutively and make them appear like a single input stream. Each input stream is read until it reaches the end of the stream. Then, the "sequence input stream" class closes the stream and automatically switches to the next input stream. Construct --- sequenceinputstream (enumeration) creates a new sequence input stream and initializes it with the enumerated value of the specified input stream. Sequenceinputstream (inputstream, inputstream) creates a new sequence input stream, initialized to first read the input stream S1, and then read the input stream S2. 2. outputsteam
2.1 bytearrayoutputstream: stores information into a buffer in the memory. This class implements an output stream that writes data in the form of a byte array. It automatically expands when data is written to the buffer zone. You can use tobytearray () and tostring () to retrieve data. Constructor (a) --- bytearrayoutputstream () creates a new byte array output stream. (B) --- bytearrayoutputstream () creates a new byte array output stream. (C) --- bytearrayoutputstream (INT) creates a new byte array output stream with the buffer capacity of the specified size bytes. Tostring (string) converts the buffer content to a string based on the specified character encoding and converts bytes to characters. Write (byte [], Int, INT) writes the len bytes starting from offset off in the specified byte array to the output stream of the byte array. Write (INT) writes the specified byte to the output stream of the byte array. Writeto (outputstream) uses out. Write (BUF, 0, count) to call the write method of the output stream to write all the content of the output stream of the byte array to the specified output stream parameter. 2.2 fileoutputstream: An output stream that outputs data to a file or filedescriptor. Constructor (a) fileoutputstream (File Name) creates a file output stream and outputs data to the specified file object. (B) fileoutputstream (filedescriptor) creates a file output stream and outputs data to the specified file descriptor. (C) fileoutputstream (string name) creates a file output stream and outputs data to the file with the specified name. (D) fileoutputstream (string, Boolean) creates an output file by specifying the system file name. 2.3 pipedoutputstream: the pipe output stream refers to the sending end of a communication pipeline. One thread sends data through the output stream of the pipeline, while the other thread reads data through the input stream of the pipeline, which enables communication between the two threads. Constructor (a) pipedoutputstream () creates an output stream of the pipeline, which is not connected to an input stream of the pipeline. (B) pipedoutputstream (pipedinputstream) creates an output stream of the pipeline, which is connected to an input stream of the pipeline.
(2) character-oriented stream Reader/writer
A Unicode-oriented stream that reads or writes information from a stream in units of Unicode characters. Reader/writer is the abstact class. The Unicode Character-oriented stream includes the following types: 1. reader 1.1 chararrayreader: corresponds to bytearrayinputstream to implement a character buffer constructorchararrayreader (char []) that can be used as a character input stream to create a chararrayreader using a specified character array. Chararrayreader (char [], Int, INT) creates a chararrayreader1.2stringreader with a specified character array: the shard stream corresponding to stringbufferinputstream whose source is a string. Stringreader (string) creates a new string reader. 1.3 filereader: corresponds to fileinputstream 1.4 pipedreader: corresponds to pipedinputstream 2. writer 2.1 chararraywrite: corresponds to bytearrayoutputstream 2.2 stringwrite: No bytes-oriented stream2.3 filewrite: corresponds to fileoutputstream 2.4 pipedwrite: it corresponds to pipedoutputstream. 3. Conversion between two different oriented streams: 3.1 inputstreamreader and outputstreamreader: converts a byte-oriented stream into a character-oriented stream. The inputstreamreader class is a bridge between byte and the bytes stream: It reads bytes and converts them to the bytes stream according to the specified encoding method. The encoding method may be specified by the name or the default encoding method acceptable to the platform. Each call to one of the read () Methods of inputstreamreader may prompt reading one or more bytes from the basic byte input stream. To achieve higher efficiency, we consider using bufferedreader to encapsulate inputstreamreader, bufferedreader in = new bufferedreader (New inputstreamreader (system. in); for example: // input an integer from the keyboard to view plaincopy to clipboardprint?
- String S = NULL;
- Inputstreamreader Re = new inputstreamreader (system. In );
- Bufferedreader BR = new bufferedreader (re );
- Try {
- S = Br. Readline ();
- System. Out. println ("s =" + integer. parseint (s ));
- BR. Close ();
- }
- Catch (ioexception E)
- {
- E. printstacktrace ();
- }
- Catch (numberformatexception e) // this exception is thrown when the application attempts to convert a string to a numeric type that cannot be converted to an appropriate format.
- {
- System. Out. println ("the input is not a number ");
- }
String S = NULL; inputstreamreader Re = new inputstreamreader (system. in); bufferedreader BR = new bufferedreader (re); try {S = BR. readline (); system. out. println ("s =" + integer. parseint (s); BR. close ();} catch (ioexception e) {e. printstacktrace ();} catch (numberformatexception
E) // this exception is thrown when the application attempts to convert a string to a numeric type but the string cannot be converted to an appropriate format. {System. Out. println ("the input is not a number ");}
Inputstreamreader (inputstream) uses the default character encoding method to create an inputstreamreader. Inputstreamreader (inputstream, string) uses a named character encoding method to create an inputstreamreader. Outputstreamwriter writes multiple characters to an output stream and converts multiple characters to bytes based on the specified character encoding. Each outputstreamwriter combines its own chartobyteconverter, which serves as a bridge from the bytes stream to the byte stream.(3) General Usage principles of Java IO:1. Classification by data source (destination): 1. File: fileinputstream, fileoutputstream, (byte stream) filereader, filewriter (character) 2. byte []: bytearrayinputstream, bytearrayoutputstream (byte stream) 3. Char []: chararrayreader, chararraywriter (Bytes stream) 4. String: stringbufferinputstream, stringbufferouputstream (byte stream) stringreader, stringwriter (Bytes Stream) 5. network data streams: inputstream, outputstream, (byte stream) reader, writer (Bytes stream) II. output by formatting or not: 1. output to formatting: printstream, printwriter III. Buffer by traffic: 1. Buffer by traffic: bufferedinputstream, bufferedoutputstream, bufferedreader, bufferedwriter (Bytes stream) 4. Data Format: 1. binary format (as long as it cannot be determined to be plain text): inputstream, outputstream and all its sub-classes with stream termination 2. plain text format (including pure English and Chinese characters or other encoding methods); reader, writer and all its sub-classes with reader, writer subclass 5. Input and Output by input: 1. Input: reader, inputstream subclass 2. Output: writer, outputstream subclass 6. Special requirements: 1. Conversion class from stream to reader: inputstreamreader, outputstreamwriter2. Object input and output: objectinputstream, objectoutputstream3. inter-process communication: pipeinputstream, pipeoutputstream, pipereader, pipewriter4, and combined input: sequenceinputstream5. Special requirements: pushbackinputstream, pushbackreader, linenumberinputstream, and linenumberreader determine which class to use and the General Principles for constructing processes. (special requirements are not considered.) first, what is the most primitive data format? Principle 4: 2; input/output: Principle 5: 3; whether to convert the stream: Principle 6: 1st: 4; Data Source (direction) what is: Principle 1, fifth, whether to buffer: Principle 3 (Note: It is important to note whether Readline () is defined and what is better than read, write more special input or output methods) Sixth, whether to format the output: Principle 2
From: http://xu20cn.blog.51cto.com/274020/128606