Source: http://blog.csdn.net/zgljl2012/article/details/47267609
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:
Packagedata flow;Importjava.io.IOException;ImportJava.io.InputStream;Importjava.net.MalformedURLException;ImportJava.net.URL; Public classTest_inputstream {/*** Get byte stream *@paramURL *@return */ Privatestring getstream (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 voidMain (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 classTest_inputstreamreader {/** A byte stream is required before a stream of characters is obtained*/ Privatestring getstream (string url) {Try { //Get byte streamInputStream in =Newurl (url). OpenStream (); //Converts a byte stream into a character stream and specifies 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; } /** * @paramargs*/ Public Static voidMain (string[] args) {//TODO auto-generated Method StubString URL = "http://www.baidu.com"; Test_inputstreamreader Test=NewTest_inputstreamreader (); System.out.println (Test.getstream (URL)); }}
Packagedata flow;ImportJava.io.BufferedReader;Importjava.io.IOException;ImportJava.io.InputStream;ImportJava.io.InputStreamReader;Importjava.net.MalformedURLException;ImportJava.net.URL; Public classTest_bufferedreader {/** Byte stream--character stream- -the character stream of cached output*/ Privatestring getstream (string url) {Try { //Get byte streamInputStream in =Newurl (url). OpenStream (); //Converts a byte stream into a character stream and specifies a character setInputStreamReader ISR =NewInputStreamReader (In, "UTF-8"); //output a stream of characters in a row in a cached formBufferedReader 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; } /** * @paramargs*/ Public Static voidMain (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.
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.
Reprint: "Java Basics" InputStream, InputStreamReader and BufferedReader