Advertisement



< Prev
Next >



BufferedWriter



File Input/Output operations consume a lot of important resources and are time consuming. Hence, writing a chunk of character to a file is faster than writing one character a time. It speeds up the I/O process.

BufferedWriter class is used to create such buffered character stream using which a chunk of characters stored in the local buffer of BufferedWriter, is written out to a file.




Constructors :


BufferedWriter(Writer r)
This constructor creates a BufferedWriter object to write characters out of a file, accessed by a Writer
Example-:
File file= new File("D:\\Textbook.txt");
FileWriter fw= new FileWriter(file);
BufferedWriter bw= new BufferedWriter(fw);
This example has created a BufferedWriter object to write characters stored in its local buffer to a file D:\\Textbook.txt through a writer character stream, FileWriter.




Point to remember


BufferedWriter is a subclass of Writer class.




Methods for writing using BufferedWriter


Some methods that we generally use while working with BufferedWriter are shown in the table below :

Methods Description
flush()
This method flushes any data to be written out of this character output stream's buffer.
write(int c)
This method writes one character at a time to this character output stream's buffer.

write(char[])
This method writes a whole character array at a time to this character output stream's buffer.

write(String str)
This method writes a String to this character output stream's buffer.

newLine()
This method writes a line separator.

close()
This method closes this output stream and also frees any resources connected with this output stream.




Advertisement




Writing a character array and a String to a file using BufferedWriter.


In this code, we are creating a BufferedWriter object to write the characters stored in its local buffer to a file D:\\Textbook.txt through a writer character stream i.e. FileWriter.
//A program to create a buffered output Stream for writing characters.

import java.io.*;
class A
{
public static void main(String... ar)
{
char[] arr= {'H', 'e', 'l' , 'l' , 'o'};
String str="Next topic is PrintWriter";



try
{
File file= new File("TextBook9.txt");
FileWriter fw= new FileWriter(file); // This will create the file and we don't need to call createNewFile();
BufferedWriter bw = new BufferedWriter(fw);

for(char ch : arr)
bw.write(ch);	//Writing a char at a time, using for-each loop

bw.newLine();	//Writing a newline character.

bw.write(str);	////Writing a String.

bw.flush();
bw.close();
}
catch(IOException e)
{
System.out.println(e);
}

}
}



Output is -:


After the execution of this program, a file TextBook.txt is created in the D: drive, with contents -:
Hello
Next topic is PrintWriter


Program Analysis






Please share this article -




< Prev
Next >
< BufferedReader
PrintWriter>



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement

Notifications



Please check our latest addition

PYTHON


Advertisement