Java.io.SequenceInputStream.available() 方法



描述

java.io.SequenceInputStream.available() 方法返回一個估計值,表示在當前底層輸入流的下一個方法呼叫(由同一執行緒或其他執行緒呼叫)阻塞之前,可以讀取(或跳過)的位元組數。 一次讀取或跳過這麼多位元組不會阻塞,但可能會讀取或跳過較少的位元組。

宣告

以下是java.io.SequenceInputStream.available() 方法的宣告。

public int available()

引數

返回值

此方法返回一個估計值,表示在阻塞之前可以從當前底層輸入流讀取(或跳過)的位元組數;如果透過呼叫其 close() 方法關閉了此輸入流,則返回 0。

異常

IOException − 如果發生 I/O 錯誤。

示例

以下示例演示了java.io.SequenceInputStream.available() 方法的使用。

package com.tutorialspoint;

import java.io.*;

public class SequenceInputStreamDemo {
   public static void main(String[] args) {

      // create two  new strings with 5 characters each
      String s1 = "Hello";
      String s2 = "World";

      // create 2 input streams
      byte[] b1 = s1.getBytes();
      byte[] b2 = s2.getBytes();
      ByteArrayInputStream is1 = new ByteArrayInputStream(b1);
      ByteArrayInputStream is2 = new ByteArrayInputStream(b2);

      // create a new Sequence Input Stream
      SequenceInputStream sis = new SequenceInputStream(is1, is2);
      
      try {
         // determine how many bytes are available in the first stream
         System.out.println("" + sis.available());
         
         // read 10 characters, 5 from each stream
         for (int i = 0; i < 10; i++) {
            char c = (char) sis.read();
            System.out.print("" + c);
         }

         // close the stream
         sis.close();

      } catch (IOException ex) {
         ex.printStackTrace();
      }
   }
}

讓我們編譯並執行上述程式,這將產生以下結果:

5
HelloWorld
java_io_sequenceinputstream.htm
廣告
© . All rights reserved.