Java Io BufferedWriter

In this section we will discuss about the BufferedWriter class in Java.

Java Io BufferedWriter

In this section we will discuss about the BufferedWriter class in Java.

Java Io BufferedWriter

Java IO BufferedWriter

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

Constructor
Description
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

  • close() : This method is used to close the stream as well as to release the resources associated with the stream.

    Syntax : public void close() throws IOException
     
  • flush() : This method is used to flush the stream.

    Syntax : public void flush() throws IOException
     
  • newLine() : This method is used for separating a line i.e. to write characters, string, or arrays into the new line.

    Syntax : public void newLine() throws IOException
     
  • write(char[] cbuf, int off, int len) : This method is used to write the specified length of characters started at the specified position of an array of characters to the stream.

    Syntax : public void write(char[] cbuf, int off, int len) throws IOException
     
  • write(int c) : This method is used to write a character to the stream.

    Syntax : public void write(int c) throws IOException
     
  • write(String s, int off, int len) : This method is used to write the specified length of string started at the specified position of a string.

    Syntax : public void write(String s, int off, int len) throws IOException

Video: How to write to a file with BufferedWriter class?

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 :

Download Source Code