The BufferedOutputStream class is a subclass of FilterOutputStream that stores written data in an internal buffer (a protected byte array field named buf) until the buffer is full or the stream is explicitly flushed using the flush ()method. Then it writes the data onto the underlying output stream all at once. This class does not declare any new methods of its own, rather all the methods are inherited from the OutputStream class.
The BufferedIntputStream class is a subclass of FilterlnputStream which reads as many data as possible into its internal buffer (a protected byte array field named buf) in a single read () call and then each read ()call reads data from the buffer instead of the underlying stream. When the buffer runs out of data, the buffered stream refills its buffer from the underlying stream. This class does not declare any new methods of its own, rather all the methods are inherited from the InputStream class.
import java.io.*;
class BufferIOStreamExample
{
public static void main(String[] args)
{
int ch;
try
{
FileOutputStream fout=new FileOutputStream("std.dat");
BufferedOutputStream bout = new BufferedOutputStream(fout);
for(int i=1;i<=10;i++)
bout.write(i*i);
bout.close();
}
catch(IOException e) { e.printStackTrace(); }
try
{
FileInputStream fin=new FileInputStream("std.dat");
BufferedInputStream bin = new BufferedInputStream(fin);
while((ch=bin.read())!=-1)
System.out.print(ch +" ");
bin.close();
}
catch(IOException e) { e.printStackTrace(); }
}
}