[Java Basics] InputStream, InputStreamReader, BufferedReader, inputstream, and reader
In Java, the above three classes are often used to process data streams. The following describes the differences between the three classes and their usage.
- InputStream: A super class of all byte input streams. It is generally used as a subclass: FileInputStream and so on, which can output byte streams;
- InputStreamReader: a bridge between a byte stream and a two-byte stream. It can output a byte stream as a two-byte stream, and can specify character sets for the byte stream to output characters one by one;
- BufferedReader: provides a common buffer mode for text reading. readLine reads a line of text, reads text from the character input stream, and buffers each character to provide efficient reading of characters, arrays, and rows.
The following three demos (the Demo accesses the Baidu homepage to obtain the byte stream and then outputs it) to describe the functions of the three classes respectively:
Package data stream; import java. io. IOException; import java. io. inputStream; import java.net. malformedURLException; import java.net. URL; public class Test_InputStream {/*** get byte stream * @ param url * @ return */private String getStream (String url) {// get byte stream InputStream in = null; string result = ""; try {in = new URL (url ). openStream (); int tmp; while (tmp = in. read ())! =-1) {result + = (char) tmp;} catch (MalformedURLException e) {// TODO Auto-generated catch block e. printStackTrace ();} catch (IOException e) {// TODO Auto-generated catch block e. printStackTrace ();} // output byte stream return result;} public static void main (String [] args) {String URL = "http://www.baidu.com"; Test_InputStream test = new Test_InputStream (); system. out. println (test. getStream (URL ));}}
The InputStream stream connection is obtained through the URL Connection, And the read method is used to read and combine the byte streams in one byte and one byte. (If the read method returns-1, the data read ends ), returns the result in reasults.
The output is as follows:
60 33 68 79 67 84 89 80 69 32 104 116 109 62 60 33 45 45 83 84 65 84 ......
This is a Byte stream. Each number is a Byte (Byte, 8 bits). Therefore, if you want to read English, use the word to throttle, and then use the (char) to forcibly convert it, however, if there are two-byte languages such as Chinese or the character sequence set needs to be specified, InputStreamReader must be used to convert byte streams into two streams.
Package data stream; import java. io. IOException; import java. io. inputStream; import java. io. inputStreamReader; import java.net. malformedURLException; import java.net. URL; public class Test_InputStreamReader {/** before getting the primary stream, you must first throttle the word */private String getStream (String url) {try {// get the byte stream InputStream in = new URL (url ). openStream (); // convert byte streams into bytes streams and specify the character set InputStreamReader isr = new InputStreamReader (in, "UTF-8"); String res Ults = ""; int tmp; while (tmp = isr. read ())! =-1) {results + = (char) tmp;} return results;} catch (MalformedURLException e) {// TODO Auto-generated catch block e. printStackTrace ();} catch (IOException e) {// TODO Auto-generated catch block e. printStackTrace ();} return null;}/*** @ param args */public static void main (String [] args) {// TODO Auto-generated method stub String URL = "http://www.baidu.com"; Test_InputStreamReader test = new Test_InputStreamReader (); System. out. println (test. getStream (URL ));}}
First get the byte stream, then create InputStreamReader to convert byte stream, and specify its character set as UTF-8, then use forced conversion to convert the int byte read to char type, at this time, Chinese characters can be output, and the output byte stream is faster than the output byte stream. The following is the result:
Package data stream; import java. io. bufferedReader; import java. io. IOException; import java. io. inputStream; import java. io. inputStreamReader; import java.net. malformedURLException; import java.net. URL; public class Test_BufferedReader {/** byte stream -- Response stream -- cached Output Response stream */private String getStream (String url) {try {// get the byte stream InputStream in = new URL (url ). openStream (); // converts bytes into bytes streams and specifies the character set InputStreamReader isr = new InputStr EamReader (in, "UTF-8"); // outputs BufferedReader bf = new BufferedReader (isr); String results = ""; string newLine = ""; while (newLine = bf. readLine ())! = Null) {results + = newLine + "\ n";} return results;} catch (MalformedURLException e) {// TODO Auto-generated catch block e. printStackTrace ();} catch (IOException e) {// TODO Auto-generated catch block e. printStackTrace ();} return null;}/*** @ param args */public static void main (String [] args) {// TODO Auto-generated method stub String URL = "http://www.baidu.com"; Test_BufferedReader test = new Test_BufferedReader (); System. out. println (test. getStream (URL ));}}
After obtaining the response stream, you can directly cache it and retrieve it from the cache area. The speed is much faster than that of InputStreamReader. The output result is the same as the preceding one.
- Summary
When reading network data streams, you can use InputStream to obtain byte streams, InputStreamReader to convert byte streams, and BufferedReader to obtain network data streams in the form of cache output.
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.