PipedInputStream類receive()方法
- receive() 方法可在
java.io
包。 - receive() 方法用於接收一個字節的內容,當沒有更多輸入時它將阻塞。
- receive() 方法是一個非靜態方法,它隻能通過類對象訪問,如果我們嘗試使用類名訪問方法,那麽我們將得到一個錯誤。
- receive() 方法可能在接收到一個字節的數據時拋出異常。
IOException:當遇到任何輸入/輸出錯誤或管道未正確連接或流關閉時,可能會拋出此異常。
用法:
protected void receive(int byt);
參數:
int byt
– 表示接收到的字節。
返回值:
該方法的返回類型是void
,它什麽都不返回。
例:
// Java program to demonstrate the example
// of receive(int byt) method of PipedInputStream
import java.io.*;
public class ReceiveOfPIS extends PipedInputStream {
public static void main(String[] args) throws Exception {
int val = 65;
try {
// Instantiates ReceiveOfPIS and
// PipedOutputStream
ReceiveOfPIS pipe_in = new ReceiveOfPIS();
PipedOutputStream pipe_out = new PipedOutputStream();
// By using connect() method is to
// connect this pipe_in to the given pipe_out
pipe_in.connect(pipe_out);
for (int i = 0; i < 3; ++i) {
// By using write() method is to
// write the val to the stream pipe_out
pipe_out.write(val);
val++;
}
for (int i = 1; i < 4; ++i) {
// By using receive() method is to
// receive a byte
pipe_in.receive(val);
System.out.println(i + " " + "Byte Received ");
}
// By using close() method is to close
// the stream
pipe_in.close();
pipe_out.close();
} catch (Exception ex) {
System.out.println(ex.toString());
}
}
}
輸出
1 Byte Received 2 Byte Received 3 Byte Received
相關用法
- Java PipedInputStream connect()用法及代碼示例
- Java PipedInputStream close()用法及代碼示例
- Java PipedInputStream available()用法及代碼示例
- Java PipedOutputStream connect()用法及代碼示例
- Java PipedOutputStream close()用法及代碼示例
- Java PipedWriter connect()用法及代碼示例
- Java PipedReader close()用法及代碼示例
- Java PipedWriter flush()用法及代碼示例
- Java PipedReader connect()用法及代碼示例
- Java PipedOutputStream flush()用法及代碼示例
- Java PipedReader ready()用法及代碼示例
- Java PipedWriter close()用法及代碼示例
- Java PriorityQueue size()用法及代碼示例
- Java PushbackReader skip()用法及代碼示例
- Java PriorityBlockingQueue peek()用法及代碼示例
- Java Package getAnnotationsByType()用法及代碼示例
- Java PriorityBlockingQueue take()用法及代碼示例
- Java PriorityBlockingQueue iterator()用法及代碼示例
- Java PriorityBlockingQueue add()用法及代碼示例
- Java Path toAbsolutePath()用法及代碼示例
注:本文由純淨天空篩選整理自Preeti Jain大神的英文原創作品 Java PipedInputStream receive() Method with Example。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。