- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is the use of in flush() and close() methods of BufferedWriter class in Java?
The BufferedWriter class of Java is used to write stream of characters to the specified destination (character-output stream). It initially stores all the characters in a buffer and pushes the contents of the buffer to the destination, making the writing of characters, arrays and Strings efficient.
You can specify the required size of the buffer at the time of instantiating this class.
The flush() method
While you are trying to write data to a Stream using the BufferedWriter object, after invoking the write() method the data will be buffered initially, nothing will be printed.
The flush() method is used to push the contents of the buffer to the underlying Stream.
Example
In the following Java program, we are trying to print a line on the console (Standard Output Stream). Here we are invoking the write() method by passing the required String.
import java.io.BufferedWriter; import java.io.IOException; import java.io.OutputStreamWriter; public class BufferedWriterExample { public static void main(String args[]) throws IOException { //Instantiating the OutputStreamWriter class OutputStreamWriter out = new OutputStreamWriter(System.out); //Instantiating the BufferedWriter BufferedWriter writer = new BufferedWriter(out); //Writing data to the console writer.write("Hello welcome to Tutorialspoint"); } }
But, since you haven’t flushed the contents of the Buffer of the BufferedWriter nothing will be printed.
To resolve this invoke the flush() method after the executing the write().
import java.io.BufferedWriter; import java.io.IOException; import java.io.OutputStreamWriter; public class BufferedWriterExample { public static void main(String args[]) throws IOException { //Instantiating the OutputStreamWriter class OutputStreamWriter out = new OutputStreamWriter(System.out); //Instantiating the BufferedWriter BufferedWriter writer = new BufferedWriter(out); //Writing data to the console writer.write("Hello welcome to Tutorialspoint"); writer.flush(); } }
Output
Hello welcome to Tutorialspoint
- Related Articles
- What is the purpose of the flush() method of the BufferedWriter class in java?
- What is the use of FLUSH PRIVILEGES statement in MySQL?
- What is the use of default methods in Java?
- What is the importance of the Throwable class and its methods in Java?
- What is the number wrapper class and its methods in Java?
- What is the character wrapper class and its methods in Java?
- What is the use of StrictMath class in Java?\n
- What is the use of the Cleaner class in Java 9?
- Writing data to a file using BufferedWriter class in Java
- Methods of StringBuffer class in Java.
- Methods of StringBuilder class in Java.
- Methods of StringTokenizer class in Java.
- Methods of the Matcher class in Java Regex
- Methods of the Pattern class in Java Regex
- Use static Import for sqrt() and pow() methods in class Math in Java
