Java CharArrayWriter flush()方法及示例

Java CharArrayWriter flush()方法及示例

Java中 CharArrayWriter 类的 flush() 方法用于冲刷流。

语法

public void flush()

参数: 该方法不接受任何参数。

返回值: 该方法不返回任何东西。

以下程序说明了上述方法:

程序1:

// Java program to illustrate
// the flush() method
 
import java.io.*;
 
public class GFG {
    public static void main(String[] args)
        throws IOException
    {
 
        // Initializing the character array
        char[] geek = { 'G', 'E', 'E', 'K', 'S' };
 
        // Initializing the CharArrayWriter
        CharArrayWriter char_array1
            = new CharArrayWriter();
 
        for (int c = 72; c < 77; c++) {
            // Use of write(int char)
            // Writer int value to the Writer
            char_array1.write(c);
        }
 
        // Use of size() method
        System.out.println("\nSize of char_array1 : "
                           + char_array1.size());
 
        // Flushes the current stream
        char_array1.flush();
    }
}

输出

Size of char_array1 : 5

程序2:

// Java program to illustrate
// the flush() method
 
import java.io.*;
 
public class GFG {
    public static void main(String[] args)
        throws IOException
    {
 
        // Initializing the character array
        char[] geek
            = { 'G', 'O', 'P', 'A', 'L', 'L' };
 
        // Initializing the CharArrayWriter
        CharArrayWriter char_array1
            = new CharArrayWriter();
 
        for (int c = 72; c < 78; c++) {
            // Use of write(int char)
            // Writer int value to the Writer
            char_array1.write(c);
        }
 
        // Use of size() method
        System.out.println("\nSize of char_array1 : "
                           + char_array1.size());
 
        // Flushes the current stream
        char_array1.flush();
    }
}

输出

Size of char_array1 : 6

参考资料 : https://docs.oracle.com/javase/10/docs/api/java/io/CharArrayWriter.html#flush()

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

Java 参考指南