InputStream 此抽象類是表示字節輸入流的所有類的超類。
我們從輸入流中讀取數據最常用的方法基本上就是如下 3 個 read() 方法了:
1 、 read () 方法,這個方法 從輸入流中讀取數據的下一個字節。返回 0 到 255 范圍內的 int 字節值。如果因為已經到達流末尾而沒有可用的字節,則返回值 -1 。
2 、 read (byte[] b,int off,int len) 方法, 將輸入流中最多 len 個數據字節讀入 byte 數組。嘗試讀取 len 個字節,但讀取的字節也可能小於該值。以整數形式返回實際讀取的字節數。
3 、 read (byte[] b) 方法, 從輸入流中讀取一定數量的字節,並將其存儲在緩沖區數組 b 中。以整數形式返回實際讀取的字節數。
第一個方法典型的確定就是處理效率低,不是某些特殊情況,很少使用它,下面說說第 2 個方法跟第 3 個方法,第 3 個方法的本本質其實就是第 2 個方法的特殊情況, 效果等同於:
read(b, 0, b.length)
所以這里把他們放着一起討論。
從第 2 個方法的 API 文檔說明來看:“ 將輸入流中最多 len 個數據字節讀入 byte 數組。嘗試讀取 len 個字節,但讀取的字節也可能小於該值。以整數形式返回實際讀取的字節數。”,最多讀取 len 個字節,這究竟是何意? API 文檔並沒有詳細說明。是不是就意味着有可能(注意這里是有可能而不是一定,)讀取不到 len 個字節呢?答案是“是的”。雖然造成這種情況的原因是什么個人並不知道,但是我們可以通過例子來發現這種情況,下面是源代碼(由於只是簡單的示例,所以代碼也就隨便寫了):
ServerSocket 端:
- package myspider;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.net.ServerSocket;
- import java.net.Socket;
- /**
- *
- * @author mark
- */
- public class MyServerSocket {
- public static void main(String[] args) throws IOException {
- ServerSocket ss = new ServerSocket(8888);
- System.out.println("runing");
- while (true) {
- byte[] b = new byte[22480];
- int readBytes = 0;
- Socket s = ss.accept();
- InputStream is = s.getInputStream();
- while (readBytes < 22480) {
- int read = is.read(b, readBytes, 22480 - readBytes);
- System.out.println(read);
- if (read == -1) {
- break;
- }
- readBytes += read;
- }
- File f = new File("F:\\project\\bocln_nacec\\xml\\ey.xml");
- if (!f.exists()) {
- f.createNewFile();
- System.out.println("creat " + f.toString());
- }
- FileOutputStream fos = new FileOutputStream(f);
- fos.write(b, 0, readBytes);
- fos.flush();
- fos.close();
- System.out.println("complete");
- is.close();
- s.close();
- }
- }
- }
Socket 端:
- package myspider;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.net.Socket;
- import java.net.UnknownHostException;
- /**
- *
- * @author mark
- */
- public class MySocket {
- public static void main(String[] args) throws UnknownHostException, IOException {
- Socket s = new Socket("127.0.0.1", 8888);
- OutputStream os = s.getOutputStream();
- File f = new File("F:\\project\\bocln_nacec\\xml\\ye.xml");
- InputStream is = new FileInputStream(f);
- byte[] b = new byte[22480];
- int i = is.read(b);
- is.close();
- os.write(b, 0, i);
- os.flush();
- os.close();
- s.close();
- }
- }
先運行 MyServerSocket ,讓后多次運行 MySocket ,這是控制台的輸出結果( ye.xml 文件長度為 20389 ):
- runing
- 20389
- -1
- creat F:\project\bocln_nacec\xml\ey.xml
- complete
- 20389
- -1
- complete
- 20389
- -1
- complete
- 20389
- -1
- complete
- 20389
- -1
- complete
- 20389
- -1
- complete
- 20389
- -1
- complete
- 20389
- -1
- complete
- 20389
- -1
- complete
- 20389
- -1
- complete
- 20389
- -1
- complete
- 8760
- 11629
- -1
- complete
- 20389
- -1
- complete
- 20389
- -1
- complete
- 20389
- -1
- complete
- 20389
- -1
- complete
- 20389
- -1
- complete
- 20389
- -1
- complete
- 3760
- 620
- 16009
- -1
- complete
- 20389
- -1
- complete
- 20389
- -1
- complete
- 20389
- -1
- complete
- 20389
- -1
- complete
- 8760
- 11629
- -1
- complete
- 20389
- -1
- complete
- 20389
- -1
- complete
- 8760
- 11629
- -1
- complete
- 20389
- -1
- complete
- 20389
- -1
- complete
- 8760
- 11629
- -1
- complete
- 20389
- -1
- complete
- 20389
- -1
- complete
- 20389
- -1
- complete
- 20389
- -1
- complete
- 20389
- -1
- complete
- 20389
- -1
- complete
- 20389
- -1
- complete
- 20389
- -1
- complete
- 20389
- -1
- complete
- 20389
- -1
- complete
- 20389
- -1
- complete
- 20389
- -1
- complete
- 20389
- -1
- complete
- 20389
- -1
- complete
通過觀察發現,在大多數情況下,我們能夠用 is.read(b, readBytes, 22480 - readBytes) 一次性就讀完整個文件,但是還是有極少數情況,我們需要兩次(如36、37兩行)甚至兩次以上(如58、59、60)調用 is.read(b, readBytes, 22480 - readBytes) 方法才能把整個文件讀取完。這里由於文件最長只有 20389 ,所以我們能讀到的最大字節數也就是 20389 而不會是 22480 了。
那么我們怎樣寫代碼才能保證在數據流沒有到達末尾的情況下讀取到自己想要的長度的字節數據呢?我們可以這樣寫:
- int readBytes=0;
- Byte[] b=new byte[1024]//1024可改成任何需要的值
- int len=b.length;
- while (readBytes < len) {
- int read = is.read(b, readBytes, len - readBytes);
- //判斷是不是讀到了數據流的末尾 ,防止出現死循環。
- if (read == -1) {
- break;
- }
- readBytes += read;
- }