Java.io.PipedOutputStream.write() 方法



描述

java.io.PipedOutputStream.write(int b) 方法將指定的位元組寫入管道輸出流。實現了 OutputStream 的 write 方法。

宣告

以下是 java.io.PipedOutputStream.write() 方法的宣告。

public void write(int b)

引數

b - 要寫入的位元組。

返回值

此方法不返回值。

異常

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

示例

以下示例演示了 java.io.PipedOutputStream.write() 方法的用法。

package com.tutorialspoint;

import java.io.*;

public class PipedOutputStreamDemo extends PipedInputStream {
   public static void main(String[] args) {
      byte[] b = {'h', 'e', 'l', 'l', 'o'};

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

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

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

         // flush the stream
         out.flush();

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

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

FGH
java_io_pipedoutputstream.htm
廣告

© . All rights reserved.