BufferedInputStream & BufferedOutputStream in Java

Introduction to BufferedInputStream and BufferedOutputStream Classes in Java

BufferedInputStream and BufferedOutputStream are two classes in Java that are used to improve the performance of input and output operations. They do this by buffering data, which means that they store a small amount of data in memory before writing it to or reading it from the underlying stream. This can improve performance because it reduces the number of times that the underlying stream needs to be accessed.

BufferedInputStream and BufferedOutputStream can be used with any type of stream, but they are especially useful for streams that are slow to access, such as network streams or file streams.

BufferedInputStream and BufferedOutputStream are very useful classes for improving the performance of input and output operations. They are easy to use and can be used with any type of stream.

Here are some additional things to keep in mind when using BufferedInputStream and BufferedOutputStream:
  • BufferedInputStream and BufferedOutputStream are both AutoCloseable classes. This means that they can be automatically closed using the try-with-resources statement.
  • BufferedInputStream and BufferedOutputStream both have a default buffer size of 8192 bytes. However, you can specify a different buffer size when you create the object.
  • BufferedInputStream and BufferedOutputStream can be used to read and write data from and to any stream, not just files.
Methods of BufferedInputStream Class
Method Description
available() Returns an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking, which may be 0, or 0 when end of stream is detected.
close() Closes this input stream and releases any system resources associated with this stream.
mark(int readlimit) Marks the current position in this input stream. A subsequent `reset()` will attempt to reposition the stream to this point.
markSupported() Tells whether this input stream supports the `mark()` and `reset()` methods.
read() Reads a byte of data from this input stream.
read(byte[] b) Reads up to `b.length` bytes of data from this input stream into an array of bytes.
read(byte[] b, int off, int len) Reads up to `len` bytes of data from this input stream into an array of bytes, starting at offset `off` in the array.
reset() Repositions this stream to the position at which the last `mark()` was set.
skip(long n) Skips over and discards `n` bytes of data from this input stream.
Methods of BufferedOutputStream Class
Method Description
close() Closes this output stream and releases any system resources associated with this stream.
flush() Flushes this output stream and forces any buffered output bytes to be written out to the underlying device.
write() Writes a byte to this output stream.
write(byte[] b) Writes `b.length` bytes from the specified byte array to this output stream.
write(byte[] b, int off, int len) Writes `len` bytes from the specified byte array starting at offset `off` to this output stream.

Exercises

Code for Writing and Reading Data from the buffer using BufferedInputStream and BufferedOutputStream


  package practiceproject;
  import java.io.*;
  import java.util.*;
  public class democlass {
    
      public static void main(String[] args) throws IOException {
        
      File obj = new File("satish.txt");
      BufferedOutputStream bout = new BufferedOutputStream(new FileOutputStream(obj));
      bout.write(2);
      System.out.println("file writing successful");
      bout.close();
      BufferedInputStream bin = new BufferedInputStream(new FileInputStream(obj));
      int data=bin.read();
      System.out.println(data);
      bin.close();
      }
  } 

Code to read data from the buffer after skipping a byte using BufferedInputStream


  package practiceproject;
  import java.io.*;
  import java.util.*;
  public class democlass {
  
  public static void main(String[] args) throws IOException {
    
    File obj = new File("satish.txt");
    BufferedInputStream bin = new BufferedInputStream(new FileInputStream(obj));
    int data=bin.read();
    System.out.println(data);
    System.out.println(bin.skip(1));
    int data1=bin.read();
    System.out.println((char)data1);
    
  }
  }