Menu
Topics Index
...
`


Input/Output: Exploring java.io > Byte Streams >
Siva Nookala - 07 Apr 2016
BufferedOutputStream increase the writing performance of the other OutputStream objects. BufferedOutputStream internally contains a buffered byte array which stores the bytes to write in to the file. Unlike the BufferedOutputStream the other OutputStream's takes write byte by byte in to the file. Firstly they have to fetch from the resource and then they will write in to the file. But BufferedOutputStream take less time to write in to file since it has a buffer it will fetch all the bytes to the buffer and then it will perform write operation at once.

public class BufferedOutputStream
   extends FilterOutputStream
Constructors :
Constructor Description
BufferedOutputStream(OutputStream out) Creates a new buffered output stream to write data to the specified underlying output stream.
BufferedOutputStream(OutputStream out, int size) Creates a new buffered output stream to write data to the specified underlying output stream with the specified buffer size.
Methods :
Method Description
void write(int b) writes the specified byte to this buffered output stream.
void write(byte[] b, int off, int len) Writes len bytes from the specified byte array starting at offset off to the buffered output stream.
void flush() flushes the buffered output stream.
BufferedOutputStreamDemo
import java.io.*;

class BufferedOutputStreamDemo
{
    public static void main(String arg[])
    {
        File source = new File("E:JavaPrograms\\FolderOne\\fileOne.txt");
        // LINE A
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(source));
        if(source.exists())
        {
            System.out.println("File exists.");
        
            
             byte b[] = {'M','E','R','I','T',' ' ,'C','A','M','P','U','S'};
            // Writing into file fileOne
             bos.write(b); // LINE B
             bos.flush(); // LINE C
             bos.close();
        }
        else
            System.out.println("File not found.");
        //bos.close();
        
        //Now retrieving data from fileOne
        // LINE D
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(source));
        int c =0;
        while((c = bis.read())!= -1)
            System.out.print((char)c);
        bis.close();
    
    }
}
OUTPUT

File exists.
MERIT CAMPUS

DESCRIPTION

At LINE A we created a BufferedOutputStream object.
At LINE B we are writing the bytes in array b to file source
At LINE C we are flushing out the bytes in the BufferedOutputStream and ensuring no more bytes in BufferedOuputStream.
At LINE D we created a BufferedInputStream object to retrieve bytes from file source and printing them.

THINGS TO TRY
  • Comment the close method in if and else block and see the output. No ouput is displayed since the BufferedOutputStream object is still holding the file. So whenever the action is completed close the streams.
  • Write Individual characters I LOVE JAVA in to source using write(int) method.

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App