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 -- uses a buffer in the memory as InputStream.
Construct ---
(A) ByteArrayInputStream (byte []) creates 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 -- takes a String object as InputStream.
Construct ---
StringBufferInputStream (String) creates an input stream String to read 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 an object as InputStream to read the object.
Construct ---
(A) FileInputStream (File name) creates an input File stream to 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 concept of pipe. It is mainly used in the process. The pipeline input stream 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 a pipeline input stream, which is not connected to a pipeline output stream.
PipedInputStream (PipedOutputStream) creates a pipeline input stream, which is connected to a pipeline output stream.
1.5
SequenceInputStream: combines multiple inputstreams into one InputStream. The "sequential input stream" class allows applications to merge 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 byte arrays.
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 output stream of a media transcoding queue. A thread sends data through an output stream in a pipeline,
The other thread reads data through the input stream of the pipeline, which enables communication between 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 a class of abstact
Unicode-oriented streams include the following types:
1. Reader
1.1
CharArrayReader: corresponds to ByteArrayInputStream to implement a character buffer zone that can be used as a character input stream.
Constructor
CharArrayReader (char []) creates a CharArrayReader with a specified character array.
CharArrayReader (char [], int, int) creates a CharArrayReader with a specified character array
1.2
StringReader: the shard stream whose source is a string corresponding to StringBufferInputStream.
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 corresponding byte-oriented stream
2.3 FileWrite: corresponds to FileOutputStream
2.4 PipedWrite: 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 ));
Example: // input an integer from the keyboard
Java code
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 ");
}
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 (character stream)
4. String: StringBufferInputStream, StringBufferOuputStream (byte stream) StringReader, StringWriter (Bytes Stream)
5. network data streams: InputStream, OutputStream, (byte stream) Reader, and Writer (Bytes Stream)
Ii. formatted output score:
1. output to be formatted: PrintStream, PrintWriter
Iii. cache score:
1. Buffer: BufferedInputStream, BufferedOutputStream, (byte stream) BufferedReader, BufferedWriter (Bytes Stream)
Iv. Data Format:
1. binary format (as long as it cannot be determined that it is plain text): InputStream, OutputStream and all its sub-classes with Stream termination
2. plain text format (including English letters, Chinese characters, and Other encoding methods); Reader, Writer, and all subclasses with Reader and Writer
5. Input/Output score:
1. Input: Reader, a subclass of the InputStream type
2. Output: a subclass of the Writer and OutputStream types.
6. Special needs:
1. Conversion class from Stream to Reader: InputStreamReader and OutputStreamWriter
2. Object input and output: ObjectInputStream and ObjectOutputStream
3. inter-process communication: PipeInputStream, PipeOutputStream, PipeReader, and PipeWriter
4. Merge input: SequenceInputStream
5. Special requirements: PushbackInputStream, PushbackReader, LineNumberInputStream, and LineNumberReader
The general guidelines for deciding which class to use and its construction process are as follows (special requirements are not considered ):
First, consider what is the most primitive data format: Principle 4
Second, whether input or output: Principle 5
Third, whether stream conversion is required: Principle 6
Fourth, what is the data source (destination): Principle 1
Fifth, whether to buffer: Principle 3 (Note: it must be noted that readLine () is defined, and there are more special input or output methods than read and write)
Author: "quding0308 blog"