Saturday, October 5, 2024
Google search engine
HomeLanguagesJavaCharArrayWriter flush() method in Java with examples

CharArrayWriter flush() method in Java with examples

The flush() method of the CharArrayWriter class in Java is used to flush the stream. 
Syntax
 

public void flush()

Parameters: This method does not accept any parameter.
Return Value: This method does not returns anything. 
Below program illustrate the above method: 
Program 1: 
 

Java




// 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();
    }
}


Output: 

Size of char_array1 : 5

 

Program 2: 
 

Java




// 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();
    }
}


Output: 

Size of char_array1 : 6

 

Reference: https://docs.oracle.com/javase/10/docs/api/java/io/CharArrayWriter.html#flush()
 

RELATED ARTICLES

Most Popular

Recent Comments