Java.io - SPLessons

Java.io BufferedOutputStream

Home > Lesson > Chapter 3
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Java.io BufferedOutputStream

Java.io BufferedOutputStream

shape Introduction

The Java.io BufferedOutputStream is the class used to write the bytes to the directory or file , so as to get buffered output. The Java.io BufferedOutputStream performance is little bit faster compared to other writers like writing the files into drives and in many more applications.

shape Description

The Java.io BufferedOutputStream is similar to that buffered writer with tiny difference that it writes stream of bytes like data related to images, where as the buffered writer writes the characters like in the text form. The main feature of this class is to write block of characters or data at a time instead of writing data character by character like output stream.

shape Conceptual figure

Class Declaration

shape Declaration

Java.io/BufferedOutputStream class is declared below: Public class BufferedOutputStream extends FileOutputStream

shape Fields

Java.io/BufferedOutputStream class fields are:
  • Protected OutputStream out : The field represents to be filter the output stream.
  • Protected int Count : The field represents to the index to count output stream bytes.
  • Protected byte [] bu : The field represents to the internal buffer where actually the data  stored.

Class Constructors

shape Table

Constructor Description
BufferedOutputStream (OutputStream out) The function of this constructor is to create the buffered output stream and writes to represented stream.
BufferedOutputStream (OutputStream out, int n ) The function of this constructor is to create the buffered output stream with represented size of buffer.

Class Methods

shape Table

Method Description
void flush () The function of this method is to flush the specifed output stream.
void write (int b) The function of this method is to write the represented byte into output stream.
void write (byte[] b, int off, int n) By using this method it writes the data up to 'n' bytes from the output stream to an array of bytes.

Inherited Methods

shape Description

From the following classes , methods are inherited the Java.io BufferedOutputStream class.
  • Java.io.object
  • Java.io.FileOutputStream

shape Examples

Usage of void flush() method. [c]import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.ByteArrayOutputStream; import java.io.FileInputStream; import java.io.IOException; public class BufferedOutputStream { public static void main(String[] args) throws Exception { FileInputStream is = null; BufferedInputStream bis = null; ByteArrayOutputStream baos = null; BufferedOutputStream bos = null; try{ // To read data from the test.txt file then open the file input stream is = new FileInputStream("D:/test.txt"); // To convert input stream into buffered input stream bis = new BufferedInputStream(is); // creating a new byte array output stream baos = new ByteArrayOutputStream(); // creating a new buffered output stream to write byte array bos = new BufferedOutputStream(baos); int value; // the file is read till to the end while ((value = bis.read()) != -1) { bos.write(value); } // invokes flush method to force bytes to be written out to baos bos.flush(); // every byte is readed from baos for (byte b: baos.toByteArray()) { // converts byte to character char c = (char)b; System.out.print(c); } }catch(IOException e) { // if IOException occurs e.printStackTrace(); }finally { // releases all the system resources associated with this stream if(is!=null) is.close(); if(bis!=null) bis.close(); if(baos!=null) baos.close(); if(bos!=null) bos.close(); } } }[/c] Output Following is the result will be displayed. [c]splessons.com[/c] Usage of void write (int b) method. [c]import java.io.BufferedOutputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; public class BufferedOutputStream { public static void main(String[] args) throws Exception { ByteArrayOutputStream baos = null; BufferedOutputStream bos = null; try{ // creating new byte array baos = new ByteArrayOutputStream(); // creating new BufferedOutputStream with baos bos = new BufferedOutputStream(baos); // Initialize the integer int b = 83; // write to stream bos.write(b); // force the byte for written to baos bos.flush(); // To convert ByteArrayOutputStream to bytes byte[] bytes = baos.toByteArray(); // print the byte System.out.println(bytes[0]); }catch(IOException e) { // if I/O error occurs. e.printStackTrace(); }finally { // releases all the system resources associated with this stream if(baos!=null) baos.close(); if(bos!=null) bos.close(); } } }[/c] Output Following is the result will be displayed.
83

Summary

shape Key Points

  • The buffered output stream is the class used to write the bytes to the directory or file , so as to get buffered output.
  • The buffered output stream performance is little bit faster compared to other writers like writing the files into drives and in many more applications.
  • The main feature of this class is to write block of characters or data at a time instead of writing data character by character like output stream.