
- Java.io - Home
- Java.io - BufferedInputStream
- Java.io - BufferedOutputStream
- Java.io - BufferedReader
- Java.io - BufferedWriter
- Java.io - ByteArrayInputStream
- Java.io - ByteArrayOutputStream
- Java.io - CharArrayReader
- Java.io - CharArrayWriter
- Java.io - Console
- Java.io - DataInputStream
- Java.io - DataOutputStream
- Java.io - File
- Java.io - FileDescriptor
- Java.io - FileInputStream
- Java.io - FileOutputStream
- Java.io - FilePermission
- Java.io - FileReader
- Java.io - FileWriter
- Java.io - FilterInputStream
- Java.io - FilterOutputStream
- Java.io - FilterReader
- Java.io - FilterWriter
- Java.io - InputStream
- Java.io - InputStreamReader
- Java.io - LineNumberInputStream
- Java.io - LineNumberReader
- Java.io - ObjectInputStream
- Java.io - ObjectInputStream.GetField
- Java.io - ObjectOutputStream
- io - ObjectOutputStream.PutField
- Java.io - ObjectStreamClass
- Java.io - ObjectStreamField
- Java.io - OutputStream
- Java.io - OutputStreamWriter
- Java.io - PipedInputStream
- Java.io - PipedOutputStream
- Java.io - PipedReader
- Java.io - PipedWriter
- Java.io - PrintStream
- Java.io - PrintWriter
- Java.io - PushbackInputStream
- Java.io - PushbackReader
- Java.io - RandomAccessFile
- Java.io - Reader
- Java.io - SequenceInputStream
- Java.io - SerializablePermission
- Java.io - StreamTokenizer
- Java.io - StringBufferInputStream
- Java.io - StringReader
- Java.io - StringWriter
- Java.io - Writer
- Java.io package Useful Resources
- Java.io - Discussion
Java - BufferedWriter close() method
Description
The Java BufferedWriter close() method flushes the characters from the stream and then closes it. After closing, further write(), append() or flush() invocations will throw an IOException.
close() method is used to close the writer and release any system resources associated with it. It ensures that any buffered data is flushed to the underlying stream before closing. Once a BufferedWriter is closed, further write operations are not allowed, and attempting them will result in an IOException.
Declaration
Following is the declaration for java.io.BufferedWriter.close() method.
public Writer close()
Parameters
NA
Return Value
This method does not return any value.
Exception
IOException − If an I/O error occurs.
Example - Using close() method
The following example shows the usage of Java BufferedWriter close() method.
BufferedWriterDemo.java
package com.tutorialspoint; import java.io.BufferedWriter; import java.io.IOException; import java.io.StringWriter; public class BufferedWriterDemo { public static void main(String[] args) throws IOException { StringWriter sw = null; BufferedWriter bw = null; try{ // create string writer sw = new StringWriter(); //create buffered writer bw = new BufferedWriter(sw); // append character. bw.append("1"); // close the writer bw.close(); // print before appending one more character System.out.println(sw.getBuffer()); // appending after closing will throw error bw.append("2"); // print after appending one more character System.out.println(sw.getBuffer()); }catch(IOException e){ // if I/O error occurs System.out.print("Cannot append, buffered writer is closed"); }finally{ // releases any system resources associated with the stream if(sw!=null) sw.close(); if(bw!=null) bw.close(); } } }
Output
Let us compile and run the above program, this will produce the following result −
1 Cannot append, buffered writer is closed
Example - Writing to a File and Closing the Writer
The following example shows the usage of Java BufferedWriter close() method.
BufferedWriterDemo.java
package com.tutorialspoint; import java.io. BufferedWriter; import java.io.FileWriter; import java.io.IOException; public class BufferedWriterDemo { public static void main(String[] args) { String filePath = "example.txt"; String content = "Hello, World!\nThis is a BufferedWriter example."; // Initialize BufferedWriter with a FileWriter try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath))) { // Write content to the file writer.write(content); System.out.println("Content written to file."); } catch (IOException e) { System.err.println("An error occurred: " + e.getMessage()); } } }
Output
Let us compile and run the above program, this will produce the following result −
Content written to file.
File Output
The content "Hello, World!\nThis is a BufferedWriter example." is written to example.txt, and the writer is automatically closed.
Explanation
A BufferedWriter is created to write to a file (example.txt) using a FileWriter.
The try-with-resources statement is used to ensure that the BufferedWriter is automatically closed after writing.
The close() method is implicitly called when the try block exits, flushing any buffered data to the file and releasing resources.
This ensures that the content ("Hello, World!\nThis is a BufferedWriter example.") is properly saved to the file.
Example - Closing the Writer Explicitly Without try-with-resources
The following example shows the usage of Java BufferedWriter close() method.
BufferedWriterDemo.java
package com.tutorialspoint; import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; public class BufferedWriterDemo { public static void main(String[] args) { String filePath = "example.txt"; String content = "BufferedWriter close() method example."; BufferedWriter writer = null; try { // Initialize BufferedWriter with a FileWriter writer = new BufferedWriter(new FileWriter(filePath)); // Write content to the file writer.write(content); System.out.println("Content written to file."); } catch (IOException e) { System.err.println("An error occurred: " + e.getMessage()); } finally { // Close the BufferedWriter explicitly if (writer != null) { try { writer.close(); System.out.println("BufferedWriter closed successfully."); } catch (IOException e) { System.err.println("Error while closing the BufferedWriter: " + e.getMessage()); } } } } }
Output
Let us compile and run the above program, this will produce the following result −
Content written to file. BufferedWriter closed successfully.
File Output
The content "BufferedWriter close() method example." is written to example.txt, and the writer is explicitly closed.
Explanation
A BufferedWriter is explicitly created without using try-with-resources.
The finally block ensures that the close() method is called regardless of whether an exception occurs in the try block.
If the writer is not null, the close() method flushes any remaining data to the file and releases resources.
Proper exception handling ensures that even if an error occurs while closing, it is logged.
Key Points About close()
Flushes Buffered Data− The close() method ensures that all buffered data is written to the underlying stream before closing.
Releases Resources− It releases system resources associated with the writer.
Error Prevention− Always use close() or try-with-resources to avoid resource leaks.
Further Writing Forbidden− After calling close(), any write operation on the writer will throw an IOException.