Java.io.OutputStreamWriter.flush() 方法



描述

java.io.OutputStreamWriter.flush() 方法刷新流。

声明

以下是java.io.OutputStreamWriter.flush() 方法的声明。

public void flush()

参数

返回值

此方法不返回值。

异常

IOException − 如果发生 I/O 错误。

示例

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

package com.tutorialspoint;

import java.io.*;

public class OutputStreamWriterDemo {
   public static void main(String[] args) {
      try {
         // create a new OutputStreamWriter
         OutputStream os = new FileOutputStream("test.txt");
         OutputStreamWriter writer = new OutputStreamWriter(os);

         // create a new FileInputStream to read what we write
         FileInputStream in = new FileInputStream("test.txt");

         // write something in the file
         writer.write(70);

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

         // read what we write
         System.out.println("" + (char) in.read());

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

让我们编译并运行上述程序,这将产生以下结果:

Flushing Stream...
Stream flushed.
F
java_io_outputstreamwriter.htm
广告