輸入流之頂層抽象InputStream

該類是全部二進制輸入流的的抽象父類數組

類中主要方法解釋說明以下測試

(1)public abstract int read() throws IOException;spa

  該方法是從輸入流中讀取下一個字節,返回的值字節是一個範圍從0到255之間的int數。若是讀到輸入流的未尾,則返回-1。 同時該方法會一直阻塞直到輸入流中數據可用,或者流讀完畢或者拋出異常。code

 

下面以FileInputStream來舉個示例。blog

讀取一個文件test.txt. 該文件內容「abcdefghijklmn」ip

  @Test
    public void testRead() throws IOException {
        FileInputStream inputStream = new FileInputStream("E:\\360downloads\\wpcache\\srvsetwp\\test.txt");
        while (true) {
            int read = inputStream.read();
            if (read == -1) {
                break;
            }
            System.out.println(read + "," + (char) read);
        }
    }

 

打印結果以下: rem

97,a
98,b
99,c
100,d
101,e
102,f
103,g
104,h
105,i
106,j
107,k
108,l
109,m
110,n

 

(2)public int read(byte b[]) throws IOException;get

  從輸入流中讀取一些字節存儲到b數組中,返回實際讀取的長度。若是b的大小爲0,則不會從輸入流中讀取字節,否現讀取的第一個字節放入b[0],第二個字節放b[1] ,依次類推,該方法也是阻塞的,直到流讀可用,或者讀完,或者拋異常。input

 

示例1:  it

   @Test
    public void testReadBytes() throws IOException {
        FileInputStream inputStream = new FileInputStream("E:\\360downloads\\wpcache\\srvsetwp\\test.txt");
        byte[] buf = new byte[100];
        int read = inputStream.read(buf);
        System.out.println(read);  //14
        String readContent = new String(buf, 0, read);
        System.out.println(readContent); //abcdefghijklmn
    }

 

示例2:

 @Test
    public void testReadBytes() throws IOException {
        FileInputStream inputStream = new FileInputStream("E:\\360downloads\\wpcache\\srvsetwp\\test.txt");
        byte[] buf = new byte[5];
        int read = inputStream.read(buf);
        System.out.println(read);  //5
        String readContent = new String(buf, 0, read);
        System.out.println(readContent); //abcde
    }

 

示例3:

 @Test
    public void testReadBytes() throws IOException {
        FileInputStream inputStream = new FileInputStream("E:\\360downloads\\wpcache\\srvsetwp\\test.txt");
        byte[] buf = new byte[5];
        int len = 0;
        while ((len = inputStream.read(buf)) != -1) {
            String readContent = new String(buf, 0, len);
            System.out.println(readContent);
        }

    }

示例3打印結果:

abcde
fghij
klmn

 

由示例3可知,read(byte b[])方法,每次都會從輸入流中讀取b.length個字節,下次讀流時,接着上一次的未尾開始。

(3)public long skip(long n) throws IOException;

   從輸入流中跳過n個字節或者說是丟棄掉n個字節

  @Test
    public void testSkip() throws IOException {
        FileInputStream inputStream = new FileInputStream("E:\\360downloads\\wpcache\\srvsetwp\\test.txt");
        byte[] buf = new byte[14];
        long skip = inputStream.skip(4); 
        System.out.println("skip:"+ skip); // skip:4

        int read = inputStream.read(buf);
        System.out.println("read:"+read); //read:10

        String readContent = new String(buf, 0, read);
        System.out.println(readContent); //efghijklmn
    }

從上面的打印結果可知,skip確實丟棄掉了4個字節(abcd), 後面read時直接從輸入流中的第5個字節開始讀取,因此最終打印是「efghijklmn」

 

(4)public int available() throws IOException ;

    檢測輸入流中還能夠read讀取的字節個數

 @Test
    public void testAvailable() throws IOException {
        FileInputStream inputStream = new FileInputStream("E:\\360downloads\\wpcache\\srvsetwp\\test.txt");

        int available = inputStream.available();
        System.out.println("第一次測試available:"+available);  //第一次測試available:14

        long skip = inputStream.skip(4);
        System.out.println("skip:" + skip); // skip:4

        available = inputStream.available();
        System.out.println("第二次測試available:"+available); // 第二次測試available:10

        byte[] buf = new byte[5];
        int read = inputStream.read(buf);
        System.out.println("read:" + read); //read:10

        available = inputStream.available();
        System.out.println("第三次測試available:"+available);  //第三次測試available:5

        String readContent = new String(buf, 0, read);
        System.out.println(readContent); //efghijklmn
    }

由打印結果,能夠很容易的理解available()方法的含義。

第1次 inputStream中可讀字節14;skip丟棄掉4個字節後,第2次 檢測 inputStream中可讀字節等於 14 - 4 = 10 個; 而後從inputStream中read讀取5個字節後,第3次打印 只剩5個可用字節了。

 

(5)public synchronized void mark(int readlimit) ;

  

先來作一個測試:

 @Test
    public void testMarks() throws IOException {
        FileInputStream inputStream = new FileInputStream("E:\\360downloads\\wpcache\\srvsetwp\\test.txt");

        byte[] buf = new byte[5];
        int read = inputStream.read(buf);
        // 第1次從inputStream流中讀5個字節
        System.out.println(new String(buf,0,read));
        // 打個標識
        inputStream.mark(5);
        // 第2次從inputStream流中讀取5個字節
        int secondRead = inputStream.read(buf);
        System.out.println(new String(buf,0,secondRead));

        // 第3次從inputStream流中讀取4個字節(只剩下4個字節了)
        int thirdRead = inputStream.read(buf);
        //注意:這裏不要用buf.length, 由於本次沒有5個字節,不能將第2次讀取到buf數組中的值所有覆蓋
        System.out.println(new String(buf,0,thirdRead));
    }

本次實驗,第1次讀取inputStream中的前5個字節 , 第2次讀取inputStream中的6到10個字節, 第3次讀取inputStream中未尾4個字節 , remark()方法好像沒有啥用吧。好像還真的是,不過請先看看markSupported()方法

請看下面測試:

 @Test
    public void testMarkSupport() throws IOException {
        FileInputStream inputStream = new FileInputStream("E:\\360downloads\\wpcache\\srvsetwp\\test.txt");
        boolean b = inputStream.markSupported();
        System.out.println(b); //false
    }

上次測試打印false ,  表示FileInputStream這種類型的流,根本就不支持mark()或者reset()方法。因此,尷尬。。。

下面找一個支持mark(),reset()的輸入流是測試一把

 @Test
    public void testMarkAndReSet() throws Exception{
        String content = "abcdefghijklmn";
        InputStream inputStream = new ByteArrayInputStream(content.getBytes());

        System.out.println("是否支持mark:"+ inputStream.markSupported()); //是否支持mark:true
        byte[] buf = new byte[5];
        int read = inputStream.read(buf);
        // 第1次從inputStream流中讀5個字節
        System.out.println(new String(buf,0,read));  //abcde
        // 打個標識
        inputStream.mark(5);
        // 第2次從inputStream流中讀取5個字節
        int secondRead = inputStream.read(buf);
        System.out.println(new String(buf,0,secondRead)); // fghij

        // 重置
        inputStream.reset();

        // 第3次從inputStream流中讀取5個字節
        int thirdRead = inputStream.read(buf);
        System.out.println(new String(buf,0,thirdRead));  // fghij

        // 第4次從inputStream流中讀取5個字節
        int forthRead = inputStream.read(buf);
        System.out.println(new String(buf,0,forthRead)); //klmn


    }

 

哈,仔細觀察打印結果,發現mark+reset後, 能夠重複讀取流中數據 。 其實,mark+reset就是這麼點功能。

好了,InputStream這個輸入流的頂層抽象類就寫完了,主要也就這麼幾個方法!

相關文章
相關標籤/搜索