flush(): This method is available in the java.io.BufferedWriter class of Java.
Syntax:
void java.io.BufferedWriter.flush() throws IOException
This method flushes the stream.
Parameters: NA
Returns: NA
Throws:
IOException - If an I/O error occurs
Approach 1: When no exception
Java
import java.io.BufferedWriter;import java.io.FileWriter;import java.io.IOException;public class BufferedWriterflush {public static void main(String[] args) throws IOException {FileWriter fileWriter = new FileWriter("D:\\hello.txt");BufferedWriter bufferedWriter =new BufferedWriter(fileWriter);bufferedWriter.flush();System.out.println("Successfully flushes buffer stream");bufferedWriter.close();}}
Output:
Successfully flushes buffer stream
Approach 2: IOException
Java
import java.io.BufferedWriter;import java.io.FileWriter;import java.io.IOException;public class BufferedWriterflush {public static void main(String[] args) throws IOException {FileWriter fileWriter = new FileWriter("D:\\hello.txt");BufferedWriter bufferedWriter =new BufferedWriter(fileWriter);bufferedWriter.close();bufferedWriter.flush();System.out.println("Successfully flushes buffer stream");}}
Output:
Exception in thread "main" java.io.IOException: Stream closed at java.base/java.io.BufferedWriter.ensureOpen(BufferedWriter.java:107) at java.base/java.io.BufferedWriter.flushBuffer(BufferedWriter.java:117) at java.base/java.io.BufferedWriter.flush(BufferedWriter.java:256)
No comments:
Post a Comment