0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Java SE 11 パイプでOutputStreamとInputStreamを接続する

Last updated at Posted at 2021-12-28
        /* インスタンス */
        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()

#参考
PipedOutputStream
PipedInputStream

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?