Inputstream, inputstreamreader, bufferedreader

Source: Internet
Author: User

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?
  1. Import java. Io .*;
  2. Public class main {
  3. Public static
    Void main (string [] ARGs ){
  4. Try {
  5. Fileinputstream FCM = new fileinputstream ("D: // desktop // test.txt ");
  6. Int I;
  7. Try {
  8. While (I = Fi. Read ())! =-1 ){
  9. System. Out. println (I );
  10. }
  11. /* Assume that test.txt contains only one text in the header "example" and its header is utf8.
  12. * Export
  13. * 233
  14. 153
  15. 179
  16. */
  17. } Catch (ioexception e ){
  18. // Todo auto-generated Catch Block
  19. E. printstacktrace ();
  20. }
  21. } Catch (filenotfoundexception e ){
  22. // Todo auto-generated Catch Block
  23. E. printstacktrace ();
  24. }
  25. }
  26. }

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?
  1. Import java. Io .*;
  2. Public class main {
  3. Public static
    Void main (string [] ARGs) throws filenotfoundexception, unsupportedencodingexception {
  4. Fileinputstream FCM = new fileinputstream ("D: // desktop // test.txt ");
  5. Try {
  6. Inputstreamreader ISR = new inputstreamreader (FCM, "utf8 ");
  7. Int I;
  8. While (I = ISR. Read ())! =-1 ){
  9. System. Out. println (char) I );
    // Outputs the token
  10. }
  11. } Catch (exception e ){
  12. // Todo auto-generated Catch Block
  13. E. printstacktrace ();
  14. }
  15. }
  16. }

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?
  1. Import java. Io .*;
  2. Public class main {
  3. Public static
    Void main (string [] ARGs) throws filenotfoundexception, unsupportedencodingexception {
  4. Fileinputstream FCM = new fileinputstream ("D: // desktop // test.txt ");
  5. Try {
  6. Inputstreamreader ISR = new inputstreamreader (FCM, "utf8 ");
  7. Bufferedreader BR = new bufferedreader (ISR );
  8. String line;
  9. While (line = Br. Readline ())! =
    Null ){
  10. System. Out. println (line );
  11. }
  12. } Catch (exception e ){
  13. // Todo auto-generated Catch Block
  14. E. printstacktrace ();
  15. }
  16. }
  17. }

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.

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.