The Wayback Machine - https://web.archive.org/web/20241111154802/https://www.geeksforgeeks.org/bufferedwriter-close-method-in-java-with-examples/
Open In App

BufferedWriter close() method in Java with Examples

Last Updated : 28 May, 2020
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

The close() method of BufferedWriter class in Java is used to flush the characters from the buffer stream and then close it. Once the stream is closed further calling the methods like write() and append() will throw the exception.

Syntax:

public void close()

Parameters: This method does not accept any parameter.

Return value: This method does not return any value.

Exceptions: This method throws IOException if an I/O error occurs.

Below programs illustrate close() method in BufferedWriter class in IO package:

Program 1:




// Java program to illustrate
// BufferedWriter close() method
  
import java.io.*;
  
public class GFG {
    public static void main(String[] args)
    {
        try {
  
            // Create the string Writer
            StringWriter stringWriter
                = new StringWriter();
  
            // Convert stringWriter to
            // bufferedWriter
            BufferedWriter buffWriter
                = new BufferedWriter(
                    stringWriter);
  
            // Write "GEEKS" to buffered writer
            buffWriter.write(
                "GEEKSFORGEEKS", 0, 5);
  
            // Close the buffered writer
            buffWriter.close();
  
            System.out.println(
                stringWriter.getBuffer());
        }
        catch (Exception e) {
            System.out.println(
                "BufferedWriter is closed");
        }
    }
}


Output:

GEEKS

Program 2:




// Java program to illustrate
// BufferedWriter close() method
  
import java.io.*;
  
public class GFG {
    public static void main(String[] args)
    {
        try {
  
            // Create the string Writer
            StringWriter stringWriter
                = new StringWriter();
  
            // Convert stringWriter to
            // bufferedWriter
            BufferedWriter buffWriter
                = new BufferedWriter(
                    stringWriter);
  
            // Write "GEEKS" to buffered writer
            buffWriter.write(
                "GEEKSFORGEEKS", 0, 5);
  
            // Close the buffered writer
            buffWriter.close();
  
            System.out.println(
                stringWriter.getBuffer());
  
            // It will throw exception
            buffWriter.write(
                "GEEKSFORGEEKS", 5, 8);
        }
        catch (Exception e) {
            System.out.println(
                "BufferedWriter is closed");
        }
    }
}


Output:

GEEKS
BufferedWriter is closed

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



Similar Reads

BufferedWriter flush() method in Java with Examples
The flush() method of BufferedWriter class in Java is used to flush the characters from the buffered writer stream. Syntax: public void flush() throws IOException Specified By: This method is specified by the flush() method of Flushable interface. Overrides: This method overrides the flush() method of Writer class. Parameters: This method does not
2 min read
BufferedWriter newLine() method in Java with Examples
The newLine() method of BufferedWriter class in Java is used to separate the next line as a new line. It is used as a write separator in buffered writer stream. Syntax: public void newLine() throws IOException Parameters: This method does not accept any parameter. Return value: This method does not return any value. Exceptions: This method throws I
2 min read
BufferedWriter write() method in Java with Examples
The write() method in BufferedWriter class in Java is used in three ways: 1. The write(int) method of BufferedWriter class in Java is used to write a single character at a time in the buffer writer stream. Syntax: public void write(int ch) throws IOException Overrides: This method overrides the write() method of Writer class. Parameters: This metho
4 min read
Java.io.BufferedWriter class methods in Java
Bufferreader class writes text to character-output stream, buffering characters.Thus, providing efficient writing of single array, character and strings. A buffer size needs to be specified, if not it takes Default value. An output is immediately set to the underlying character or byte stream by the Writer.Class Declaration public class BufferedWri
5 min read
PrintStream close() method in Java with Examples
The close() method of PrintStream Class in Java is used to close the stream. Closing a stream deallocates any value in it or any resources associated with it. The PrintStream instance once closed won't work. Also a PrintStream instance once closed cannot be closed again. Syntax: public void close() Parameters: This method do not accepts any paramet
2 min read
Scanner close() method in Java with Examples
The close() method ofjava.util.Scanner class closes the scanner which has been opened. If the scanner is already closed then on calling this method, it will have no effect. Syntax: public void close() Return Value: The function does not return any value. Below programs illustrate the above function: Program 1: // Java program to illustrate the // c
1 min read
PushbackReader close() method in Java with Examples
The close() method of PushbackReader Class in Java is used to close the stream and release the resources that were busy in the stream, if any. This method has following results: If the stream is open, it closes the stream releasing the resources If the stream is already closed, it will have no effect. If any read or other similar operation is perfo
2 min read
PrintWriter close() method in Java with Examples
The close() method of PrintWriter Class in Java is used to close the stream. Closing a stream deallocates any value in it or any resources associated with it. The PrintWriter instance once closed won't work. Also a PrintWriter instance once closed cannot be closed again. Syntax: public void close() Parameters: This method do not accepts any paramet
2 min read
Writer close() method in Java with Examples
The close() method of Writer Class in Java is used to close the writer. Closing a writer deallocates any value in it or any resources associated with it. The Writer instance once closed won't work. Also a Writer instance once closed cannot be closed again. Syntax: public void close() Parameters: This method do not accepts any parameter. Return Valu
2 min read
StringWriter close() method in Java with Examples
The close() method oStringWriter Class in Java is used to close the writer. Closing a writer deallocates any value in it or any resources associated with it. The StringWriter instance once closed won't work. Also a StringWriter instance once closed cannot be closed again. Syntax: public void close() Parameters: This method does not accepts any para
2 min read
Reader close() method in Java with Examples
The close() method of Reader Class in Java is used to close the stream and release the resources that were busy in the stream, if any. This method has following results: If the stream is open, it closes the stream releasing the resources If the stream is already closed, it will have no effect. If any read or other similar operation is performed on
2 min read
CharArrayReader close() method in Java with Examples
The close() method of CharArrayReader Class in Java is used to close the stream and release the resources that were busy in the stream, if any. This method has following results: If the stream is open, it closes the stream releasing the resources If the stream is already closed, it will have no effect. If any read or other similar operation is perf
3 min read
StringReader close() method in Java with Examples
The close() method of StringReader Class in Java is used to close the stream and release the resources that were busy in the stream, if any. This method has following results: If the stream is open, it closes the stream releasing the resources If the stream is already closed, it will have no effect. If any read or other similar operation is perform
2 min read
ByteArrayInputStream close() method in Java with Examples
The close() method is a built-in method of the Java.io.ByteArrayInputStream closes the input stream and releases system resources associated with this stream to Garbage Collector. Syntax: public void close() Parameters: The function does not accepts any parameter. Return Value: The function returns nothing. Below is the implementation of the above
2 min read
Formatter close() method in Java with Examples
The close() method is a built-in method of the java.util.Formatter which closes the formatter. In case it is already closed, it will have no effect. Closing it means that it will release the resources that it is already holding. Syntax: public void close() Parameters: The function accepts no parameter. Return Value: The function returns nothing, it
2 min read
ObjectInputStream close() method in Java with examples
The close() method of the ObjectInputStream class in Java closes the input stream. Syntax: public void close() 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 program to illustrate // the above method import java.io.*; public cl
1 min read
CharArrayWriter close() method in Java with examples
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 Code // Java program t
2 min read
BufferedInputStream close() method in Java with Examples
The close() method of BufferedInputStream class in Java closes the input stream and releases any system resources associated with it. Once the close() method is called, reading from any input file is banned and the system will throw an IOException. To tackle the issue, the user might use a try-catch block, to catch any such exception and throw a pr
2 min read
BufferedReader close() method in Java with Examples
The close() method of BufferedReader class in Java is used to close the stream and release all the system resources associated with the stream operations. Syntax: public void close() throws IOException Parameters: This method does not accept any parameter. Return value: This method does not return any value. Exception: This method throws IOExceptio
2 min read
ByteArrayOutputSteam close() method in Java with Examples
The close() method of ByteArrayOutputStream class in Java has no effect if other methods of this class are called after closing the ByteArrayOutputStream. This method does not throw exception if even after closing the ByteArrayOutputStream, the other method is called. Syntax: public void close() Specified By: This method is specified by close() met
2 min read
PushbackInputStream close() method in Java with Examples
The close() method of PushbackInputStream class in Java is used to closes the input stream and it releases resources of system that are associated with the stream. After calling this method, further calling other method if this class will throw the IOException. Syntax: public void close() throws IOException Specified By: This method is specified by
2 min read
FileInputStream close() Method in Java with Examples
FileInputStream class is helpful to read data from a file in the form of a sequence of bytes. FileInputStream is meant for reading streams of raw bytes such as image data. For reading streams of characters, consider using FileReader. FileInputStream.close() method After any operation to the file, we have to close that file. For that purpose, we hav
2 min read
Java FileReader Class close() Method with Examples
The close() method of FileReader class in Java is used to close the file reader. We can't utilize the reader to read data after the close() method is executed. This procedure closes the stream and frees all associated system resources. Calling read(), ready(), mark(), reset(), or skip() after this method has been used will result in an IOException.
2 min read
Correct Ways to Close InputStream and OutputStream in Java with Examples
Since IO needs you to deal with streams, channels, and file descriptors that need to be closed properly, it is uncomfortable for Java developers to deal with them. Because of this, many Java programmers never bother doing clean-up, the laziness of just doing work and ignoring anything is really easy. For programmers who have never done system progr
3 min read
Why You Need to Close the Java Streams in Finally Block?
In Java finally block is a block used to execute important and common code. The finally block is mostly used during exception handling with try and catch to close streams and files. The code in finally block is executed irrespective of whether there is an exception or not. This ensures that all the opened files are properly closed and all the runni
2 min read
Java - Close AWT Window
Java Abstract Window Toolkit (AWT) is a package that creates dynamic and interactive Graphical User Interfaces (GUIs) for Java applications. The ability to properly close AWT windows is an important aspect of developing user-friendly AWT applications. In this article, we will look into different methods of how to close AWT windows along with their
7 min read
How to Create and Close JsonGenerator?
To begin with, What is JSON? JSON stands for JavaScript Object Notation . It's a data exchange format that is used between server and client. An example of JSON data: { "name" : "Sonali Singh", "age": 40, "address" : { "state": "Texas", "city": "Houston" } } JSONGenerator is an interface that writes JSON data to the output source in a streaming way
2 min read
Java 8 | ArrayDeque removeIf() method in Java with Examples
The removeIf() method of ArrayDeque is used to remove all those elements from ArrayDeque which satisfies a given predicate filter condition passed as a parameter to the method. This method returns true if some element are removed from the Vector. Java 8 has an important in-built functional interface which is Predicate. Predicate, or a condition che
3 min read
Java lang.Long.lowestOneBit() method in Java with Examples
java.lang.Long.lowestOneBit() is a built-in method in Java which first convert the number to Binary, then it looks for first set bit present at the lowest position then it reset rest of the bits and then returns the value. In simple language, if the binary expression of a number contains a minimum of a single set bit, it returns 2^(first set bit po
3 min read
Java lang.Long.numberOfTrailingZeros() method in Java with Examples
java.lang.Long.numberOfTrailingZeros() is a built-in function in Java which returns the number of trailing zero bits to the right of the lowest order set bit. In simple terms, it returns the (position-1) where position refers to the first set bit from the right. If the number does not contain any set bit(in other words, if the number is zero), it r
3 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg