/* インスタンス */
PipedOutputStream pipedOutputStream = new PipedOutputStream(); //パイプの送信側 上流
PipedInputStream pipedInputStream = new PipedInputStream(); //パイプの受信側 下流
/* 送信側と受信側の接続 */
pipedOutputStream.connect(pipedInputStream); //connect()は送信側と受信側のどちら一方から呼び出す
/* パイプへの送信 */
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(pipedOutputStream))
bufferedWriter.write("テキスト");
bufferedWriter.flush(); // flush()を呼び出すことでデータは受信側のバッファに送られる
/* パイプからの受信 */
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(pipedInputStream))
System.out.println(bufferedReader.readLine());
/* 切断 */
bufferedWriter.close() //送信側のclose()を呼び出しても受信側のバッファを読み込むことは可能
bufferedReader.close()
More than 1 year has passed since last update.
Java SE 11 パイプでOutputStreamとInputStreamを接続する
Last updated at Posted at 2021-12-28
Register as a new user and use Qiita more conveniently
- You get articles that match your needs
- You can efficiently read back useful information
- You can use dark theme