標籤:字元流 java 資料流
在Java中,上述三個類經常用於處理資料流,下面介紹一下三個類的不同之處以及各自的用法。
- InputStream : 是所有位元組輸入資料流的超類,一般使用它的子類:FileInputStream等,它能輸出位元組流;
- InputStreamReader : 是位元組流與字元流之間的橋樑,能將位元組流輸出為字元流,並且能為位元組流指定字元集,可輸出一個個的字元;
- BufferedReader : 提供通用的緩衝方式文本讀取,readLine讀取一個文本行, 從字元輸入資料流中讀取文本,緩衝各個字元,從而提供字元、數組和行的高效讀取。
下面有三個Demo(Demo訪問百度首頁擷取位元組流然後輸出)來分別說明三個類的作用:
package 資料流;import java.io.IOException;import java.io.InputStream;import java.net.MalformedURLException;import java.net.URL;public class Test_InputStream { /** * 擷取位元組流 * @param url * @return */ private String getStream(String url){ //擷取位元組流 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(); } //輸出位元組流 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)); }}
通過URL串連擷取了InputStream流串連,然後通過read方法來一個位元組一個位元組的讀取位元組流並組合在一起(read方法返回-1則資料讀取結束),最後以reasults返回。
輸出如下:
60 33 68 79 67 84 89 80 69 32 104 116 109 108 62 60 33 45 45 83 84 65 84 ……
這就是位元組流,每個數字都是一個位元組(Byte,8位),所以如果讀取英文的話,用位元組流,然後用(char)強制轉化一下就行了,但如果有中文等雙位元組語言或者說需要指定字元編碼集的情況,就必須用到InputStreamReader將位元組流轉化為字元流了。
package 資料流;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.net.MalformedURLException;import java.net.URL;public class Test_InputStreamReader { /* * 得到字元流前需先有位元組流 */ private String getStream(String url){ try { //得到位元組流 InputStream in = new URL(url).openStream(); //將位元組流轉化成字元流,並指定字元集 InputStreamReader isr = new InputStreamReader(in,"UTF-8"); String results = ""; 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)); }}
先擷取位元組流,然後建立InputStreamReader將位元組流轉化成字元流,並指定其字元集為UTF-8,然後使用強制轉化將read到的int位元組轉化為char型,此時已可以輸出中文字元,並且可速度上看出,輸出字元流比輸出位元組流要快。下面是結果的部分:
package 資料流;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 { /* * 位元組流——字元流——緩衝輸出的字元流 */ private String getStream(String url){ try { //得到位元組流 InputStream in = new URL(url).openStream(); //將位元組流轉化成字元流,並指定字元集 InputStreamReader isr = new InputStreamReader(in,"UTF-8"); //將字元流以緩衝的形式一行一行輸出 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)); }}
擷取字元流後,可直接緩衝,然後從緩衝區取出,這時的速度比InputStreamReader又將快上很多。輸出結果同上。
- 總結
在讀取網路資料流的時候,可以通過先用InputStream擷取位元組流、InputStreamReader將位元組流轉化成字元流、BufferedReader將字元流以緩衝形式輸出的方式來快速擷取網路資料流。
著作權聲明:本文為博主原創文章,未經博主允許不得轉載。
【Java基礎】InputStream 、 InputStreamReader和BufferedReader