Signup/Sign In

CharArrayWriter flush() Method in Java

In this tutorial, we will learn about the flush() method of CharArrayWriter class in Java. The flush() method of the CharArrayWriter class in Java is used to flush the stream.

Syntax

Here is the syntax of flush() method, this method does not accept any parameter and does not return anything.

public void flush()

Example 1

Here is the example of flush() method we are writing the character array to the CharArrayWriter object using write() method and it will write all the data to it. After writing the data we are calling flush() method this method flushes the stream to the underlying stream.

import java.io.CharArrayWriter;
import java.io.IOException;
class StudyTonight
{
	public static void main(String[] args) throws IOException 
	{ 
        char[] arr = {'S', 't', 'u', 'd', 'y', 't', 'o', 'n', 'i', 'g', 'h', 't'}; 
  
        CharArrayWriter charArrayWriter = new CharArrayWriter(); 
  
        for (char c: arr)
        { 
        	charArrayWriter.write(c); 
        } 
  
        System.out.println("Size of charArrayWriter : "+ charArrayWriter.size()); 
  
        charArrayWriter.flush(); 
	} 
}


Size of charArrayWriter : 12

Example 2

Here is the example of flush() method we are writing the character array to the CharArrayWriter object using append() method and it will write all the data to it. After writing the data we are calling flush() method this method flushes the stream to the underlying stream.

import java.io.CharArrayWriter;
import java.io.IOException;
class StudyTonight
{
	public static void main(String[] args) throws IOException 
	{ 

		CharArrayWriter chw = new CharArrayWriter();
       
        CharSequence csq = "Hello World";
        
        chw.append(csq);
        
        chw.flush();
        
        System.out.println(chw.toString());
	} 
}


Hello World

Conclusion:

In this tutorial, we learned about the flush() method of CharArrayWriter class in Java. The flush() method of the CharArrayWriter class in Java is used to flush the stream.



About the author:
I am the founder of Studytonight. I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development.