Java.io.Writer.flush() 方法



描述

java.io.Writer.flush() 方法重新整理流。如果流在緩衝區中儲存了來自各種 write() 方法的任何字元,則立即將它們寫入其目標位置。然後,如果該目標是另一個字元或位元組流,則將其重新整理。因此,一次 flush() 呼叫將重新整理 Writer 和 OutputStream 鏈中的所有緩衝區。

宣告

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

public abstract void flush()

引數

返回值

此方法不返回值。

異常

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

示例

以下示例顯示了 java.io.Writer.flush() 方法的使用。

package com.tutorialspoint;

import java.io.*;

public class WriterDemo {
   public static void main(String[] args) {
      String s = "Hello World";

      // create a new writer
      Writer writer = new PrintWriter(System.out);

      try {
         // append a string
         writer.append(s);

         // flush the writer
         writer.flush();

         // append a new string in a new line
         writer.append("\nThis is an example");

         // flush the stream again
         writer.close();

      } catch (IOException ex) {
         ex.printStackTrace();
      }
   }
}

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

Hello World
This is an example
java_io_writer.htm
廣告

© . All rights reserved.