"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 respective usage methods.

    • InputStream: is a superclass of all byte input streams, generally using its subclasses: FileInputStream and so on. It can output byte stream;
    • InputStreamReader: A bridge between a byte stream and a character stream that can output a stream of characters. You can also specify a character set for the byte stream, which can output a character.
    • 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.

Here are three Demos (demo visit Baidu home page 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, and each number is a single bytes (byte). 8-bit), so suppose to read English words, with a byte stream. You can then force a conversion with (char), but suppose you have a double-byte language such as Chinese, or if you need to specify a character encoding set. 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 the byte stream into a character stream. and specify the 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 to char, which is able to output Chinese characters, and can be seen at speed, The output character stream is faster than the output byte stream. The following 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 the byte stream into a character stream. and specify the 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 a character stream, you can cache it directly and then remove it from the cache. At this time the speed is much faster than the InputStreamReader. The output is as shown above.

    • Summarize
      While reading the network data stream. It can obtain the network data stream at high speed 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 cached form.

"Java Basics" InputStream, InputStreamReader, and BufferedReader

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.