Showing posts with label BufferedOutputStream. Show all posts
Showing posts with label BufferedOutputStream. Show all posts

BufferedOutputStream in Java

The BufferedOutputStream is the output counterpart of BufferedInputStream class. In BufferedOutputStream class output is buffered so no need to write data to disk every time a byte is written. Here we use flush() method to force write operation.

Example:

/**
From BufferedOutputStreams.java
*/

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;

/**
 *
 * @author JavaHotSpot
 */

public class BufferedOutputStreams
{
  public static void main(String args[])
  {
      try
      {
      String s1="This is exampleof BufferedOutputStream Class";
      byte data[]=s1.getBytes();
      FileOutputStream fo = new FileOutputStream("C:\\file1.txt");
      BufferedOutputStream buff = new BufferedOutputStream(fo);
      for(int i=0;i<data.length;i++)
      {
         buff.write(data[i]);
      }
      buff.flush();
      buff.close();
      System.out.println("Done...!");
      }
      catch(Exception ex)
      {
          System.err.println(ex);
      }
  }
}

Output: