
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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 purpose of the flush() method of the BufferedWriter class in java?
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().
Example
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 Questions & Answers
- What is the use of in flush() and close() methods of BufferedWriter class in Java?
- What is the purpose of System class in Java?
- What is the purpose of Runtime class in Java?
- What is the purpose of Process class in Java?
- What is the purpose of the StringBuilder class in C#?
- What is the purpose of overriding a finalize() method in Java?
- What is the purpose of using a dumpStack() method in Java?
- What is the purpose of using Optional.ifPresentOrElse() method in Java 9?
- What is the purpose of interfaces in java?
- What is the purpose of toString() method? Or What does the toString() method do in java?
- What is the purpose of private constructor in Java?
- What is the purpose of a constructor in java?
- What is the purpose of the command interpreter?
- What is the purpose of the testng.xml file?
- What is the purpose of the .gitignore file?
Advertisements