Java.io.BufferedWriter class methods in Java
Last Updated :
16 Aug, 2022

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.
JAVA
//Java program illustrating use of write(int arg) method
import java.io.*;
public class NewClass
{
public static void main(String[] args)
{
//initializing FileWriter
FileWriter geek_file;
try
{
geek_file = new FileWriter("ABC.txt");
// Initializing BufferedWriter
BufferedWriter geekwrite = new BufferedWriter(geek_file);
System.out.println("Buffered Writer start writing :)");
// Use of write() method to write the value in 'ABC' file
// Printing E
geekwrite.write(69);
// Printing 1
geekwrite.write(49);
// Closing BufferWriter to end operation
geekwrite.close();
System.out.println("Written successfully");
}
catch (IOException except)
{
except.printStackTrace();
}
}
}
- Note : 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.
JAVA
//Java program illustrating use of write(String arg, int offset, int length) method
import java.io.*;
public class NewClass
{
public static void main(String[] args)
{
//Initializing a FileWriter
FileWriter geek_file;
try
{
geek_file = new FileWriter("ABC.txt");
// Initializing a BufferedWriter
BufferedWriter geekwrite = new BufferedWriter(geek_file);
System.out.println("Buffered Writer start writing :)");
String arg = "Hello Geeks";
int offset = 6;
geekwrite.write(arg,offset,arg.length()-offset);
// Closing Buffer
geekwrite.close();
System.out.println("Written successfully");
}
catch (IOException except)
{
except.printStackTrace();
}
}
}
- Note : 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.
Buffered Writer start writing :)
Written successfully
- newLine() : java.io.BufferedWriter.newLine() breaks/separates line.
Syntax :
public void newLine()
Return :
Doesn't return any value.
JAVA
//Java program explaining use of newLine() method
import java.io.*;
public class NewClass
{
public static void main(String[] args)
{
//initializing FileWriter
FileWriter geek_file;
try
{
geek_file = new FileWriter("ABC.txt");
// Initializing BufferedWriter
BufferedWriter geekwrite = new BufferedWriter(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 line
geekwrite.newLine();
// Printing "FOR"
geekwrite.write("FOR");
// For next line
geekwrite.newLine();
// Printing "GEEKS"
geekwrite.write("FOR");
// Closing BufferWriter to end operation
geekwrite.close();
System.out.println("Written successfully");
}
catch (IOException except)
{
except.printStackTrace();
}
}
}
- Note :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 |
| 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
//Java program illustrating use of flush(), close() method
import java.io.*; //BufferedWriter, FileWriter, IOException
public class NewClass
{
public static void main(String[] args)
{
FileWriter geek_file; //initializing FileWriter
try
{
geek_file = new FileWriter("ABC.txt");
// Initializing BufferedWriter
BufferedWriter geekwrite = new BufferedWriter(geek_file);
System.out.println("Buffered Writer start writing :)");
// Use of write() method to write the value in 'ABC' file
geekwrite.write(69); // Printing E
geekwrite.newLine(); // For next line
geekwrite.write(49); // Printing 1
// flush() method : flushing the stream
geekwrite.flush();
// close() method : closing BufferWriter to end operation
geekwrite.close();
System.out.println("Written successfully");
}
catch (IOException except)
{
except.printStackTrace();
}
}
}
- Note : 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
Similar Reads
Java.io.BufferedInputStream class in Java
A BufferedInputStream adds functionality to another input stream-namely, the ability to buffer the input and to support the mark and reset methods. When the BufferedInputStream is created, an internal buffer array is created. As bytes from the stream are read or skipped, the internal buffer is refil
4 min read
java.nio.Buffer Class in Java
The Buffer class provides a buffer or a container for data chunks of specific primitive types. A finite sequence of elements is stored linearly in a buffer. Important properties of a buffer that make it convenient to perform read and write operations in the data are: Capacity: This property determin
4 min read
BufferedWriter close() method in Java with Examples
The close() method of BufferedWriter class in Java is used to flush the characters from the buffer stream and then close it. Once the stream is closed further calling the methods like write() and append() will throw the exception. Syntax: public void close() Parameters: This method does not accept a
2 min read
Java.io.ByteArrayInputStream class in Java
ByteArrayInputStream class of java.io package contains all the buffers, containing bytes to be read from the Input Stream. There is no IO exception in case of ByteArrayInputStream class methods. Methods of this class can be called even after closing the Stream, there is no effect of it on the class
3 min read
BufferedReader mark() method in Java with Examples
The mark() method of BufferedReader class in Java is used to mark the current position in the buffer reader stream. The reset() method of the same BufferedReader class is also called subsequently, after the mark() method is called. The reset() method fixes the position at the last marked position so
3 min read
BufferedReader close() method in Java with Examples
The close() method of BufferedReader class in Java is used to close the stream and release all the system resources associated with the stream operations. Syntax: public void close() throws IOException Parameters: This method does not accept any parameter. Return value: This method does not return a
2 min read
BufferedReader Class lines() method in Java with Examples
BufferedReader.lines() is the method of the Java Buffered Reader Class in the Java Library which returns lines in terms of Stream and from this Buffered Reader class. With the help of the stream, there are a lot of methods that mimic the output according to our needs. Syntax: BufferedReader.lines()
2 min read
BufferedReader reset() method in Java with Examples
The reset() method of BufferedReader class in Java is used to fix or mark the position at the last marked position so that the same byte can be read again. Syntax: public void reset() throws IOException Overrides: It overrides the reset() method of Reader class. Parameters: The method does not accep
3 min read
CharBuffer get() methods in Java
The get() method of java.nio.CharBuffer Class is used to reads the char at the given buffer's current position, and then increments the position. Syntax: public abstract char get() Return Value: This method returns the char value at the buffer's current position. Exceptions: This method throws Buffe
5 min read
Java.io.Writer class in Java
This abstract class for writing to character streams. The only methods that a subclass must implement are write(char[], int, int), flush(), and close(). Most subclasses, however, will override some of the methods defined here in order to provide higher efficiency, additional functionality, or both.
4 min read