Address: http://blog.csdn.net/moxie008/article/details/5663488
Http://blog.csdn.net/xiaoya629/article/details/5610670
Http://blog.csdn.net/hippoppower/article/details/4547876
. Inputstream, outputstream
Abstract class for processing byte streams
Inputstream is the superclass of all classes of the byte input stream. We generally use its subclass, such as fileinputstream.
Outputstream is the superclass of all classes of the byte output stream. We generally use its subclass, such as fileoutputstream.
2. inputstreamreader outputstreamwriter
Abstract class used to process the livestream
Inputstreamreader is a bridge between byte stream and byte stream.
Outputstreamwriter is a bridge between the bytes stream and the byte stream. It converts the bytes stream to a byte stream.
3. bufferedreader bufferedwriter
Bufferedreader is extended by the reader class and provides a general buffer mode for text reading. Readline reads a text line,
Read text from the character input stream, buffer each character, so as to provide efficient reading of characters, arrays and rows.
Bufferedwriter is extended by the writer class and provides a general buffer mode for text writing. newline uses its own line separator,
Writes text to the character output stream to buffer each character, providing efficient writing of a single character, array, and string.
Inputstream can read a byte from the source,
So it is the lowest level,
Example:
[Java]
View plaincopyprint?
- Import java. Io .*;
- Public class main {
- Public static
Void main (string [] ARGs ){
- Try {
- Fileinputstream FCM = new fileinputstream ("D: // desktop // test.txt ");
- Int I;
- Try {
- While (I = Fi. Read ())! =-1 ){
- System. Out. println (I );
- }
- /* Assume that test.txt contains only one text in the header "example" and its header is utf8.
- * Export
- * 233
- 153
- 179
- */
- } Catch (ioexception e ){
- // Todo auto-generated Catch Block
- E. printstacktrace ();
- }
- } Catch (filenotfoundexception e ){
- // Todo auto-generated Catch Block
- E. printstacktrace ();
- }
- }
- }
Import Java. io. *; <br/> public class main {<br/> Public static void main (string [] ARGs) {</P> <p> try {<br/> fileinputstream FCM = new fileinputstream ("D: // desktop // test.txt"); <br/> int I; </P> <p> try {<br/> while (I = Fi. read ())! =-1) {<br/> system. out. println (I); <br/>}< br/>/* assume that test.txt contains only one text ", the output is utf8 <br/> * exported <br/> * 233 <br/> 153 <br/> 179 <br/> */<br/>} catch (ioexception E) {<br/> // todo auto-generated Catch Block <br/> E. printstacktrace (); <br/>}</P> <p>} catch (filenotfoundexception E) {<br/> // todo auto-generated Catch Block <br/> E. printstacktrace (); <br/>}</P> <p >}< br/>}
Inputstreamreader
Inputstreamreader encapsulates inputstream,
It reads one character at a time in a more advanced mode,
The following example assumes that there is a document encoded as utf8,
The content contains only one text "simplified"
[Java]
View plaincopyprint?
- Import java. Io .*;
- Public class main {
- Public static
Void main (string [] ARGs) throws filenotfoundexception, unsupportedencodingexception {
- Fileinputstream FCM = new fileinputstream ("D: // desktop // test.txt ");
- Try {
- Inputstreamreader ISR = new inputstreamreader (FCM, "utf8 ");
- Int I;
- While (I = ISR. Read ())! =-1 ){
- System. Out. println (char) I );
// Outputs the token
- }
- } Catch (exception e ){
- // Todo auto-generated Catch Block
- E. printstacktrace ();
- }
- }
- }
Import Java. io. *; <br/> public class main {<br/> Public static void main (string [] ARGs) throws filenotfoundexception, unsupportedencodingexception {</P> <p> fileinputstream FCM = new fileinputstream ("D: // desktop // test.txt "); <br/> try {<br/> inputstreamreader ISR = new inputstreamreader (FCM, "utf8"); <br/> int I; <br/> while (I = ISR. read ())! =-1) {<br/> system. out. println (char) I); // extract the cursor <br/>}< br/>} catch (exception E) {<br/> // todo auto-generated Catch Block <br/> E. printstacktrace (); <br/>}</P> <p >}< br/>}
Bufferedreader
Bufferedreader is more advanced than inputstreamreader,
It encapsulates the streamreader class,
Read the characters of a row at a time.
[Java]
View plaincopyprint?
- Import java. Io .*;
- Public class main {
- Public static
Void main (string [] ARGs) throws filenotfoundexception, unsupportedencodingexception {
- Fileinputstream FCM = new fileinputstream ("D: // desktop // test.txt ");
- Try {
- Inputstreamreader ISR = new inputstreamreader (FCM, "utf8 ");
- Bufferedreader BR = new bufferedreader (ISR );
- String line;
- While (line = Br. Readline ())! =
Null ){
- System. Out. println (line );
- }
- } Catch (exception e ){
- // Todo auto-generated Catch Block
- E. printstacktrace ();
- }
- }
- }
Certificate ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
12.4 conversion stream -- outputstreamwriter class and inputstreamreader class
The entire Io package is actually divided into byte stream and byte stream, but in addition to these two streams, there is also a set of byte stream-transform class.
Outputstreamwriter: A child of writer. It converts the output bytes stream into a byte stream, that is, the output object of a bytes stream into a byte stream output object.
Inputstreamreader: a sub-class of reader. It converts the input byte stream into a bytes stream, that is, the input object of a byte stream into the input object of the bytes stream.
If the file operation is used as an example, the character data in the memory must be converted into a byte stream through outputstreamwriter before it can be saved in the file. During reading, the byte that is read must flow through inputstreamreader into a bytes stream, the conversion steps are 12-7.
|
(Click to view the larger image) Figure 12-7 conversion steps |
From the figure 12-7, we can clearly find that, no matter how you operate, all are stored in the file in bytes.