"Java Basics" InputStream, InputStreamReader, and BufferedReader

Source: Internet
Author: User
Tags getstream

In Java, the above three classes are often used to process data streams, and the following describes the differences between the three classes and their usage.

    • InputStream: is a superclass of all byte input streams, generally using its subclasses: FileInputStream, etc., it can output byte stream;
    • InputStreamReader: It is a bridge between the byte stream and the character stream, can output the character stream as a character stream, and can specify the character set for the byte stream, and can output one of the characters;
    • BufferedReader: Provides a common buffered text read, ReadLine reads a line of text, reads text from the character input stream, buffers individual characters, and provides efficient reading of characters, arrays, and rows.

Below are three demos (demo visit Baidu home page to get byte stream and then output) to explain the role of three classes respectively:

    • InputStream
 PackageData flow;ImportJava.io.IOException;ImportJava.io.InputStream;ImportJava.net.MalformedURLException;ImportJava.net.URL; Public  class test_inputstream {    /** * Get byte stream * @param URL * @return  * *    PrivateStringGetStream(String URL) {//Get byte streamInputStream in =NULL; String result ="";Try{in =NewURL (URL). OpenStream ();inttmp while(TMP = In.read ())! =-1) {result + = (Char) TMP; }        }Catch(Malformedurlexception e) {//TODO auto-generated catch blockE.printstacktrace (); }Catch(IOException e) {//TODO auto-generated catch blockE.printstacktrace (); }//Output byte stream        returnResult } Public Static void Main(string[] args) {String URL ="Http://www.baidu.com"; Test_inputstream test =NewTest_inputstream ();    System.out.println (Test.getstream (URL)); }}

A inputstream stream connection is obtained through a URL connection, and then a byte-byte read stream is fetched by the Read method and combined (the Read method returns 1 to the end of the data read), and the last is returned as Reasults.

The output is as follows:

60 33 68 79 67 84 89 80 69 32 104 116 109 108 62 60 33 45 45 83 84 65 84 ...

This is the byte stream, each number is a bytes (byte,8 bit), so if you read English, use the byte stream, and then use (char) to force a conversion, but if there is a double-byte language such as Chinese or need to specify the character encoding set of cases, You must use the InputStreamReader to stream the bytes into a character flow.

    • InputStreamReader
 PackageData flow;ImportJava.io.IOException;ImportJava.io.InputStream;ImportJava.io.InputStreamReader;ImportJava.net.MalformedURLException;ImportJava.net.URL; Public  class test_inputstreamreader {    / * * A byte stream is required before getting a stream of characters * /    PrivateStringGetStream(String URL) {Try{//Get byte streamInputStream in =NewURL (URL). OpenStream ();//Convert a byte stream into a character stream and specify a character setInputStreamReader ISR =NewInputStreamReader (In,"UTF-8"); String results ="";inttmp while(TMP = Isr.read ())! =-1) {Results + = (Char) TMP; }returnResults }Catch(Malformedurlexception e) {//TODO auto-generated catch blockE.printstacktrace (); }Catch(IOException e) {//TODO auto-generated catch blockE.printstacktrace (); }return NULL; }/** * @param args * *     Public Static void Main(string[] args) {//TODO auto-generated method stubString URL ="Http://www.baidu.com"; Test_inputstreamreader test =NewTest_inputstreamreader ();    System.out.println (Test.getstream (URL)); }}

Get the byte stream first, then create a inputstreamreader to convert the byte stream into a character stream, and specify its character set as UTF-8, and then use the cast to convert the read to the int byte into a char type, when the Chinese characters can be output, and can be seen at speed, The output character stream is faster than the output byte stream. Here is the part of the result:

    • BufferedReader
 PackageData flow;ImportJava.io.BufferedReader;ImportJava.io.IOException;ImportJava.io.InputStream;ImportJava.io.InputStreamReader;ImportJava.net.MalformedURLException;ImportJava.net.URL; Public  class test_bufferedreader {    /* Byte stream--character stream- -cache output Character stream * /    PrivateStringGetStream(String URL) {Try{//Get byte streamInputStream in =NewURL (URL). OpenStream ();//Convert a byte stream into a character stream and specify a character setInputStreamReader ISR =NewInputStreamReader (In,"UTF-8");//A stream of characters is output in the form of a cached lineBufferedReader BF =NewBufferedReader (ISR); String results =""; String NewLine =""; while((NewLine = Bf.readline ())! =NULL) {results + = newline+"\ n"; }returnResults }Catch(Malformedurlexception e) {//TODO auto-generated catch blockE.printstacktrace (); }Catch(IOException e) {//TODO auto-generated catch blockE.printstacktrace (); }return NULL; }/** * @param args * *     Public Static void Main(string[] args) {//TODO auto-generated method stubString URL ="Http://www.baidu.com"; Test_bufferedreader test =NewTest_bufferedreader ();    System.out.println (Test.getstream (URL)); }}

After you get the character stream, you can cache it directly and then remove it from the buffer, which is much faster than InputStreamReader. The output is as shown above.

    • Summarize
      When reading the network data stream, we can get the network data stream quickly by first using InputStream to get the byte stream, InputStreamReader to convert the byte stream into character stream, and bufferedreader the character stream to output in cache form.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

"Java Basics" InputStream, InputStreamReader, and BufferedReader

Related Article

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.