Detailed IO system in JavaCategory: Java Development Application Notes (reading, experience)2009-03-04 11:2646118 People readComments (PNS)Collection ReportJavaiostreamconstructorstringbyte
List of related reading notes and articles of experience
The Java stream is processed into a character stream and a byte stream. A character stream processes a cell that is 2-byte Unicode characters, manipulating characters, character arrays, or strings, and a byte-stream processing unit of 1 bytes, manipulating bytes and byte arrays.
In Java, characters are stored in Unicode encoding, and the character stream processing class is responsible for converting externally encoded character streams and the stream of Unicode characters within Java. Whereas class InputStreamReader and OutputStreamWriter handle the conversion of character streams and byte streams. Character stream (one buffer can be processed at a time) one operation is more efficient than a byte stream (one bytes at a time).
(i) byte-oriented stream------Inputstream/outputstream
InputStream and OutputStream are two abstact classes, and for byte-oriented streams all extend these two ribs (base class ^_^);
1, InputStream
1.1
Bytearrayinputstream--Uses a buffer in memory as a inputstream.
Construct---
A Bytearrayinputstream (byte[]) creates a new byte array input stream (Bytearrayinputstream) that 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 that reads data from the specified byte array.
---Mark: This byte array is not copied.
1.2
StringBufferInputStream--Put a String object as a inputstream.
Construct---
StringBufferInputStream (string) creates an input stream string that reads data from a specified string.
Note: Using the StringBufferInputStream method is not recommended. This class cannot convert the characters correctly to bytes.
Similar to JDK version 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 a inputstream to read the file
Construct---
A FileInputStream (file name) creates an input file stream that reads data from the specified file object.
B FileInputStream (filedescriptor) creates an input file stream that reads data from the specified file descriptor.
(C)-fileinputstream (String name) creates an input file stream that reads data from a file of the specified name.
Method----Read () reads a byte of data from the current input stream.
Read (byte[]) reads B.length bytes of data from the current input stream into a single byte array.
Read (byte[], int, int) reads the Len byte data from the input stream into a single byte array.
1.4
PipedInputStream: The concept of pipe is realized, which is used in the main thread. Pipeline inflow refers to the receiving end of a communication pipeline.
One thread sends data through the pipeline output stream, while another thread reads the data through a pipeline input, which enables communication between two threads.
Construct---
PipedInputStream () creates a pipeline input stream that is not yet connected to a pipeline output stream.
PipedInputStream (PipedOutputStream) creates a pipeline input stream that is connected to a pipeline output stream.
1.5
Sequenceinputstream: Merge multiple inputstream into one inputstream. The sequence input stream class allows the application to combine several input streams in succession.
And make them appear as a single input stream. Each input stream is read sequentially until the end of the stream is reached.
The sequence input stream class then 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 enumeration value of the specified input stream.
Sequenceinputstream (InputStream, InputStream) creates a new sequence input stream, initializes the read input stream s1 first, and then reads the input stream s2.
2, Outputsteam
2.1
Bytearrayoutputstream: The information is stored in a buffer in memory. This class implements an output stream that writes data as a byte array.
It expands automatically when the data is written to the buffer. Data can be retrieved using Tobytearray () and toString ().
Constructor
(a) Create a new byte array output stream---bytearrayoutputstream ().
(B) Create a new byte array output stream---bytearrayoutputstream ().
(C)---bytearrayoutputstream (int) Creates a new byte array output stream with buffer capacity of the specified size byte.
ToString (String) converts the buffer contents to a string based on the specified character encoding, and converts the bytes to characters.
Write (byte[], int, int) writes the byte array output stream to the specified byte array, starting with a Len byte from offset off.
Write (int) writes the specified bytes to the byte array output stream.
WriteTo (OutputStream) invokes the write method of the output stream with Out.write (buf, 0, Count) to write the entire contents of the byte array output stream to the specified output stream parameters.
2.2
FileOutputStream: The file output stream is an output stream that outputs data to file or FileDescriptor.
Constructor
A FileOutputStream (file name) creates a file output stream that outputs data to the specified file object.
B FileOutputStream (filedescriptor) creates a file output stream that outputs data to the specified file descriptor.
C FileOutputStream (String name) creates a file output stream that outputs data to a file of the specified name.
D FileOutputStream (String, Boolean) creates an output file with the file name of the specified system.
2.3
PipedOutputStream: The pipeline output stream refers to the sending side of a communication pipeline. A thread sends data through a pipeline output stream,
The other thread reads the data through a piped stream, which enables communication between two threads.
Constructor
A PipedOutputStream () creates a pipeline output stream that has not yet been connected to a pipeline's inflow.
B PipedOutputStream (PipedInputStream) creates a pipeline output stream that is connected to a pipeline input stream.
(ii) character-oriented stream Reader/writer
A Unicode-oriented stream that represents a Unicode character that reads from or writes information to a stream.
Reader/writer for Abstact class
The Unicode character-oriented stream includes the following types:
1. Reader
1.1
CharArrayReader: Corresponds to a bytearrayinputstream that implements a character buffer that can be used as a character input stream
Constructor
CharArrayReader (char[]) creates a chararrayreader with the specified character array.
CharArrayReader (char[], int, int) creates a chararrayreader with the specified character array
1.2
StringReader: A stream of characters 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: corresponding to Bytearrayoutputstream
2.2 Stringwrite: No corresponding byte-oriented stream
2.3 FileWrite: corresponding to FileOutputStream
2.4 Pipedwrite: corresponding to PipedOutputStream
3. Conversion between two different directed stream
3.1
InputStreamReader and Outputstreamreader:
Converts a byte-oriented stream into a character-oriented stream.
The InputStreamReader class is a bridge that flows from a byte to a character stream: it reads in bytes and converts it to a character stream based on the specified encoding.
The encoding used may be specified by name, or the platform can accept the default encoding method.
Each invocation of one of the InputStreamReader's read () methods may cause one or more bytes to be read from the base byte input stream.
In order to achieve greater efficiency, consider packaging inputstreamreader with BufferedReader,
BufferedReader in = new BufferedReader (new InputStreamReader (system.in));
For example://Implement input an integer from the keyboard
[Java] View plaincopy
String s =NULL;
InputStreamReader re =NewInputStreamReader (system.in);
BufferedReader br =NewBufferedReader (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 an application attempts to convert a string to a numeric type, but the string cannot be converted to the appropriate format.
{
SYSTEM.OUT.PRINTLN ("Input is not a number");
}
InputStreamReader (InputStream) creates a inputstreamreader with the default character encoding.
InputStreamReader (InputStream, String) creates a inputstreamreader with a named character encoding.
OutputStreamWriter writes multiple characters to an output stream, converting multiple characters to bytes according to the specified character encoding.
Each outputstreamwriter merges its own chartobyteconverter, and thus is the bridge from the character stream to the word stream.
(c) General principles of Use of Java IO:
First, according to the data source (whereabouts) Classification:
1, is the file: FileInputStream, FileOutputStream, (Byte stream) FileReader, FileWriter (character)
2, is byte[]: Bytearrayinputstream, Bytearrayoutputstream (Byte stream)
3, is char[]: CharArrayReader, Chararraywriter (character stream)
4, is String:stringbufferinputstream, Stringbufferouputstream (Byte stream) StringReader, StringWriter (character stream)
5, network Data flow: InputStream, OutputStream, (Byte stream) Reader, Writer (character stream)
Second, according to whether to format the output points:
1. To format the output: PrintStream, PrintWriter
Third, press whether to buffer points:
1, to buffer: Bufferedinputstream, Bufferedoutputstream, (Byte stream) BufferedReader, BufferedWriter (character stream)
Iv. by Data format:
1. binary format (as long as it cannot be determined to be plain text): InputStream, OutputStream and all subclasses with Stream end
2, plain text format (including pure English and Chinese characters or other encoding); reader, writer and all the subclasses of reader, writer
Five, according to the input and output points:
1. Input: Reader, subclass of InputStream type
2. Output: Writer, subclass of OutputStream type
Vi. Special Needs:
1. Conversion class from Stream to Reader,writer: InputStreamReader, OutputStreamWriter
2, object input and output: ObjectInputStream, ObjectOutputStream
3. Interprocess communication: Pipeinputstream, Pipeoutputstream, Pipereader, Pipewriter
4. Merge input: Sequenceinputstream
5, more special needs: Pushbackinputstream, Pushbackreader, Linenumberinputstream, LineNumberReader
The general guidelines for deciding which class to use and its construction process are as follows (regardless of special needs):
First, consider what the most primitive data format is: Principle Four
The second is the input or output: Principle five
Third, do you need to convert the flow: Principle Six 1th
What is the source of data (whereabouts): Principle One
Five, whether to buffer: principle Three (Special note: It is important to pay attention to the readLine () whether there is a definition, what is more than read, write a more special input or output method)
VI, do you want to format the output: Principle two
Detailed IO system in Java