In this section we will discuss about the BufferedWriter class in Java.
java.io.BufferedWriter class extends the java.io.Writer class. BufferedWriter class is used to write to the stream from buffer. Generally, to increase the efficiency of writing to the output stream this class is used. This class creates a buffer from where the output streams are written. Constructors of this class provides the facility to create a default size buffer and/or a buffer with the specified size. This class provides a method newLine() for line separator.
Constructor Detail
| BufferedWriter(Writer out) | This constructor creates a new BufferedWriter with the default size of
character output stream. Syntax : public BufferedWriter(Writer out) |
| BufferedWriter(Writer out, int sz) | This constructor creates a new BufferedWriter with the specified size of
character output stream. Syntax : public BufferedWriter(Writer out, int sz) |
Method Detail
Example
This example explains that contents of an existing file will be read first and then these contents will be written to the new file. For this I have created a Java class named JavaBufferedWriterExample.java. In this example I have created objects of FileReader and BufferedReader, FileReader object is wrapped within BufferedReader, these are used to read data from the source. Then to write these data to the output stream created objects of FileWriter BufferedWriter. FileWriter object is wrapped within BufferedWriter to enhance the writing efficiency.
Source Code
JavaBufferedWriterExample.java
import java.io.BufferedWriter;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class JavaBufferedWriterExample
{
public static void main(String args[])
{
BufferedWriter bw = null;
BufferedReader br = null;
FileReader fr = null;
FileWriter fw = null;
try
{
fr = new FileReader("abc.txt");
br = new BufferedReader(fr);
fw = new FileWriter("xyz.txt");
bw = new BufferedWriter(fw);
int r;
System.out.println();
System.out.println("******* Contents of abc.txt file has been written into the xyz.txt
***********");
System.out.println();
while((r= br.read()) != -1)
{
bw.write(r);
}
System.out.println();
}
catch(IOException e)
{
System.out.println("IOException caught..!!");
e.printStackTrace();
}
finally
{
if(bw != null)
{
try
{
bw.close();
}
catch(IOException iex1)
{
System.out.println(iex1);
}
}
if(fr != null)
{
try
{
fr.close();
}
catch (IOException ioe)
{
System.out.println(ioe);
}
}
if(br != null)
{
try
{
br.close();
}
catch(IOException iex)
{
System.out.println(iex);
}
}
if(fw != null)
{
try
{
fw.close();
}
catch(IOException ie1)
{
System.out.println(ie1);
}
}
}// end finally
}// end main
}// end class
Output
Before executing the above example a file (say, as abc.txt is in this example) contained the source must be available at the specified location or directory. When you will execute the above code you will get the output as follows :

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.
Ask Questions? Discuss: Java Io BufferedWriter
Post your Comment