Java.io.PipedInputStream.read() 方法



描述

java.io.PipedInputStream.read() 方法讀取此管道輸入流中的下一個位元組資料。值位元組作為 0 到 255 範圍內的 int 返回。此方法會阻塞,直到有輸入資料可用、檢測到流的結尾或丟擲異常。

宣告

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

public int read()

引數

返回值

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

異常

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

示例

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

package com.tutorialspoint;

import java.io.*;

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

      // create a new Piped input and Output Stream
      PipedOutputStream out = new PipedOutputStream();
      PipedInputStream in = new PipedInputStream();

      try {
         // connect input and output
         in.connect(out);

         // write something 
         out.write(70);
         out.write(71);

         // read what we wrote
         for (int i = 0; i < 2; i++) {
            System.out.println("" + (char) in.read());
         }
      } catch (IOException ex) {
         ex.printStackTrace();
      }
   }
}

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

F
G
java_io_pipedinputstream.htm
廣告