Advertisement



< Prev
Next >



BufferedOutputStream



File Input/Output operations consume a lot of important resources and are time consuming. Hence, writing a chunk of bytes to a file is faster than writing one byte at a time. It speeds up the I/O process.

BufferedOutputStream class is used to create such buffered output stream using which a chunk of bytes stored in the local buffer of BufferedOutputStream, is written out to a file.




Constructors :


BufferedOutputStream(OutputStream os)
This constructor creates a BufferedOutputStream object to write a chunk of byte to a file, which is accessed by an OutputStream.

Example-:
File file= new File("D:/Textbook.txt");
FileOutputStream fos= new FileOutputStream(file);
BufferedOutputStream fis= new BufferedOutputStream(fos);
This example has created a BufferedOutputStream object to write the bytes stored in its local buffer to a file D:/Textbook.txt through OutputStream i.e. FileOutputStream, passed to constructor.




Point to remember


FileOutputStream is a subclass of OutputStream




Methods for writing using BufferedOutputStream


Some methods that we generally use while working with BufferedOutputStream are shown in the table below :

Methods Description
flush()
This method flushes any buffered data to be written out of this output stream.
write(int b)
This method writes one byte at a time to this output stream.

write(byte[])
This method writes a whole byte array at a time to this output stream.

close()
This method closes this outut stream and also frees any resources connected with this output stream.




Advertisement




Program to write to a file using BufferedOutputStream.


In this code, we are creating a BufferedOutputStream object to write the bytes stored in its local buffer to a file D:/Textbook.txt through OutputStream i.e. FileOutputStream.
//Program to create a BufferedIOutputStream to write(one byte at a time) out to an existing file.

import java.io.*;
class A
{
public static void main(String... ar)
{

String str="Java is the best programming language";
byte b[]=str.getBytes();

try
{
FileOutputStream fos= new FileOutputStream("D://TextBook.txt");
BufferedOutputStream bos= new BufferedOutputStream(fos); ///A local buffer is created which is big-
							//enough to hold a chunk of bytes to be
							//written out to a file TextBook.txt
							
bos.write(b); //writes to local buffer
bos.flush();
bos.close();
fos.close();
}
catch(IOException e)
{
System.out.println(e);
}

}
}


Output is -:


A file called "TextBook.txt" is created in D: drive, with the contents -:
Java is the best programming language


Program Analysis






Please share this article -




< Prev
Next >
< BufferedInputStream
SequenceInputStream>



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement