Java.io.SequenceInputStream.read() 方法



描述

java.io.SequenceInputStream.read() 方法讀取此輸入流中的下一個位元組資料。該位元組以 0 到 255 範圍內的 int 值返回。如果由於到達流的末尾而沒有可用的位元組,則返回 -1 值。此方法會阻塞,直到輸入資料可用、檢測到流的末尾或丟擲異常。

宣告

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

public int read()

引數

返回值

此方法返回下一個位元組資料,如果到達流的末尾則返回 -1。

異常

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

示例

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

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 {
         // read 10 characters, 5 from each stream
         for (int i = 0; i < 10; i++) {
            char c = (char) sis.read();
            System.out.print("" + c);
         }

         // change line
         System.out.println();

         // close the streams
         sis.close();

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

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

HelloWorld
java_io_sequenceinputstream.htm
廣告