Bufferreader class writes text to character-output stream, buffering characters.Thus, providing efficient writing of single array, character and strings. A buffer size needs to be specified, if not it takes Default value.
An output is immediately set to the underlying character or byte stream by the Writer.
Class Declaration
public class BufferedWriter extends Writer
Constructors
- BufferedWriter(Writer out): Creates a buffered character-output stream that uses a default-sized output buffer.
- BufferedWriter(Writer out, int size): Creates a new buffered character-output stream that uses an output buffer of the given size.
Methods:
- write() : java.io.BufferedWriter.write(int arg) writes a single character that is specified by an integer argument.
Syntax :public void write(int arg) Parameters : arg : integer that specifies the character to write Return : Doesn't return any value.
Implementation :
//Java program illustrating use of write(int arg) methodimportjava.io.*;publicclassNewClass{publicstaticvoidmain(String[] args){//initializing FileWriterFileWriter geek_file;try{geek_file =newFileWriter("ABC.txt");// Initialing BufferedWriterBufferedWriter geekwrite =newBufferedWriter(geek_file);System.out.println("Buffered Writer start writing :)");// Use of write() method to write the value in 'ABC' file// Printing Egeekwrite.write(69);// Printing 1geekwrite.write(49);// Closing BufferWriter to end operationgeekwrite.close();System.out.println("Written successfully");}catch(IOException excpt){excpt.printStackTrace();}}}chevron_rightfilter_noneNote : In the given output, you can’t see it’s action on file. Run this code on any compiler in your device. It creates a new file ‘ABC’ and write “E 1 ” in it.
Output : Buffered Writer start writing :) Written successfully
- write() : java.io.BufferedWriter.write(String arg, int offset, int length) writes String in the file according to its arguments as mentioned in the Java Code.
Syntax :public void write(String arg, int offset, int length) Parameters : arg : String to be written offset : From where to start reading the string length : No. of characters of the string to write Return : Doesn't return any value.
Implementation :
//Java program illustrating use of write(String arg, int offset, int length) methodimportjava.io.*;publicclassNewClass{publicstaticvoidmain(String[] args){//Initializing a FileWriterFileWriter geek_file;try{geek_file =newFileWriter("ABC.txt");// Initializing a BufferedWriterBufferedWriter geekwrite =newBufferedWriter(geek_file);System.out.println("Buffered Writer start writing :)");String arg ="Hello Geeks";intoffset =6;geekwrite.write(arg,offset,arg.length()-offset);// Closing Buffergeekwrite.close();System.out.println("Written successfully");}catch(IOException except){except.printStackTrace();}}}chevron_rightfilter_noneNote : In the given output, you can’t see it’s action on file. Run this code on any compiler in your device. It creates a new file ‘ABC’ and write “Geeks” in it.Here,
arg = Hello Geeks offset = 6 length = arg.length So, when we minus offset : 6, it will write 'Geeks' only in the file.
Output:
Buffered Writer start writing :) Written successfully
- newLine() : java.io.BufferedWriter.newLine() breaks/separates line.
Syntax :
public void newLine() Return : Doesn't return any value.
Implementation :
//Java program explaining use of newLine() methodimportjava.io.*;publicclassNewClass{publicstaticvoidmain(String[] args){//initializing FileWriterFileWriter geek_file;try{geek_file =newFileWriter("ABC.txt");// Initialing BufferedWriterBufferedWriter geekwrite =newBufferedWriter(geek_file);System.out.println("Buffered Writer start writing :)");// Use of write() method to write the value in 'ABC' file// Printing "GEEKS"geekwrite.write("GEEKS");// For next linegeekwrite.newLine();// Printing "FOR"geekwrite.write("FOR");// For next linegeekwrite.newLine();// Printing "GEEKS"geekwrite.write("FOR");// Closing BufferWriter to end operationgeekwrite.close();System.out.println("Written successfully");}catch(IOException excpt){excpt.printStackTrace();}}}chevron_rightfilter_noneNote :In the given output, you can’t see it’s action on file. Run this code on any compiler in your device. It creates a new file ‘ABC’ and write write
| GEEKS |
| FOR |
| GEEKS | Here, newLine() method breaks line after GEEKS and FOR is written in next line
Output :Buffered Writer start writing :) Written successfully
- flush() : java.io.BufferedWriter.flush() flushes character from write buffer.
Syntax :public void flush() Return : Doesn't return any value.
- close() : java.io.BufferedWriter.close() flushes character from write buffer and then close it.
Syntax :public void close() Return : Doesn't return any value.
Implementation of flush(), close() method :
//Java program illustrating use of flush(), close() methodimportjava.io.*;//BufferedWriter, FileWriter, IOExceptionpublicclassNewClass{publicstaticvoidmain(String[] args){FileWriter geek_file;//initializing FileWritertry{geek_file =newFileWriter("ABC.txt");// Initialing BufferedWriterBufferedWriter geekwrite =newBufferedWriter(geek_file);System.out.println("Buffered Writer start writing :)");// Use of write() method to write the value in 'ABC' filegeekwrite.write(69);// Printing Egeekwrite.newLine();// For next linegeekwrite.write(49);// Printing 1// flush() method : flushing the streamgeekwrite.flush();// close() method : closing BufferWriter to end operationgeekwrite.close();System.out.println("Written successfully");}catch(IOException excpt){excpt.printStackTrace();}}}chevron_rightfilter_noneNote : You can’t see it’s action on file. Run this code on any compiler in your device.It creates a new file ‘ABC’ and write
| E |
| 1 |
in it.Here, flush() method flushes the stream and close() method closes the writer.Output : Buffered Writer start writing :) Written successfully
This article is contributed by Mohit Gupta 🙂. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- Static methods vs Instance methods in Java
- Java.util.BitSet class methods in Java with Examples | Set 2
- java.lang.Character class methods | Set 1
- HashMap Class Methods in Java with Examples | Set 1 (put(), get(), isEmpty() and size())
- BitSet class methods in Java with Examples | Set 3
- Java.lang.Integer class and its methods
- util.date class methods in Java with Examples
- Math class methods in Java with Examples | Set 2
- java.math class and its methods | Set 1
- java.lang.Character class - methods | Set 2
- java.lang.Boolean class methods
- java.math class and its methods | Set 3
- getproperty() and getproperties() methods of System Class in Java
- Java.io.OutputStreamWriter Class methods
- isAlive() and join() methods of Thread Class in Java
- Java.lang.Class class in Java | Set 1
- Java.lang.Class class in Java | Set 2
- Using predefined class name as Class or Variable name in Java
- Java.util.TimeZone Class (Set-2) | Example On TimeZone Class
- Implement Pair Class with Unit Class in Java using JavaTuples


