Skip to content
Related Articles

Related Articles

CharArrayWriter close() method in Java with examples
  • Last Updated : 28 May, 2019

The close() method of the CharArrayWriter class in Java is used to close the current stream.

Syntax:

public void close()

Parameters: This method does not accept any parameter.

Return Value: This method closes the current stream. However, it does not release the buffer.

Below program illustrate the above method:



Program 1:




// Java program to illustrate
// the close() method
  
import java.io.*;
  
public class GFG {
    public static void main(String[] args)
        throws IOException
    {
  
        // Initailizing the character array
        char[] geek
            = { 'G', 'E', 'E', 'K', 'S' };
  
        // Initailizing 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());
  
        // Close the current stream
        char_array1.close();
    }
}
Output:
Size of char_array1 : 5

Program 2:




// Java program to illustrate
// the close() method
  
import java.io.*;
  
public class GFG {
    public static void main(String[] args)
        throws IOException
    {
  
        // Initailizing the character array
        char[] geek
            = { 'G', 'O', 'P', 'A', 'L', 'L' };
  
        // Initailizing 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());
  
        // Close the current stream
        char_array1.close();
    }
}
Output:
Size of char_array1 : 6

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

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :