Java.io.PipedWriter.flush() 方法



描述

java.io.PipedWriter.flush() 方法重新整理此輸出流,並強制寫入任何緩衝的輸出字元。這將通知管道中等待的任何讀取器。

宣告

以下是 java.io.PipedWriter.flush() 方法的宣告。

public void flush()

引數

返回值

此方法不返回值。

異常

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

示例

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

package com.tutorialspoint;

import java.io.*;

public class PipedWriterDemo {
   public static void main(String[] args) {
   
      // create a new Piped writer and reader
      PipedWriter writer = new PipedWriter();
      PipedReader reader = new PipedReader();

      try {
         // connect the reader and the writer
         writer.connect(reader);

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

         // flush the writer
         System.out.println("Flushing writer...");
         writer.flush();
         System.out.println("Writer flushed.");

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

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

Flushing writer...
Writer flushed.
F
G
java_io_pipedwriter.htm
廣告

© . All rights reserved.