Java I/O Buffered Streams

In this section we will discuss the I/O Buffered Streams.

Java I/O Buffered Streams

In this section we will discuss the I/O Buffered Streams.

Java I/O Buffered Streams

Java I/O Buffered Streams

In this section we will discuss the I/O Buffered Streams.

In Java programming when we are doing an input and output operation then we are trying to interact our Java program to read from or write data to the memory. In the perspective of Java we can do this in two ways :

  • Unbuffered : In an unbuffered way the read and write operations are performed by the O/S directly, this may cause to much less efficiency of program due to often triggering of disk access, network activity and other relative operations.
  • Buffered : In a buffered way of programming read and write operations are done through a memory area called buffer. In the read operation input streams read data from the memory area and the native input API is called only on the empty buffer and in the write operation the output stream writes data to a buffer and the native output API is called only on the full buffer.

An unbuffered stream can be changed to the buffered stream by wrapping the buffered stream classes. There are four buffered stream classes from which two are the buffered byte stream classes and the other twos are the buffered character streams classes these are as follows :

Video: How to use Buffered Streams in Java?

Buffered Byte Streams classes

Classes Description
BufferedInputStream This class makes input stream able for buffering the input.
BufferedOutputStream This class provides the facility to write output from the buffer.

Methods of BufferedInputStream

  • available() : This method is used to get the number of bytes that can be read from the input stream.

    public int available() throws IOException

  • close() : This method is used to close the resource and the resource associated to the stream get released.

    public void close() throws IOException
     
  • mark(int readlimit) : This method is used to mark the current position in the input stream.

    public void mark(int readlimit)
     
  • markSupported() : This method is used to specify that whether the input stream supported the mark() and reset() methods.

    public boolean markSupported()
  • read() : This method is used to read the next byte of data of input stream.

    public abstract int read() throws IOException
     
  • read(byte[] b, int off, int len) : This method is used to read byte from the byte-input stream in a given byte array, started from the specified offset.

    public int read(byte[] b, int off, int len) throws IOException
     
  • reset() : This method is used to repositioned the stream where the mark method was marked the current position in the input stream.

    public void reset() throws IOException
     
  • skip(long n) : This method is used to skipped over and discarded the data of n bytes from the input stream.

    public long skip(long n) throws IOException
     

Methods of BufferedOutputStream

  • flush() : This method is used to flush the output stream.

    public void flush() throws IOException
     
  • write(byte[] b, int off, int len) : This method is used to write the specified length of bytes into the buffered output stream, started from the given offset from the given byte array.

    public void write(byte[] b, int off, int len) throws IOException
     
  • write(int b) : This method is used to write the given byte into the buffered output stream.

    public void write(int b) throws IOException
     

Buffered Character Stream Classes

Classes Description
BufferedReader This class facilitate to read texts from the character-input stream
BufferedWriter This class facilitate to write character-output stream using buffer.

Methodd of BufferedReader

  • close() : This method is used to close the input stream and the associated resources get released.

    public void close() throws IOException
     
  • mark(int readAheadLimit) : This method is used to mark the current position in the input stream.

    public void mark(int readAheadLimit) throws IOException
     
  • markSupported() : This method specifies that whether the mark() operation is supported by the stream or not.

    public boolean markSupported()
     
  • read() : This method is used to read a single character.

    public int read() throws IOException
     
  • read(char[] cbuf, int off, int len) : This method is used to read characters of an array at the specified portion.

    public int read(char[] cbuf, int off, int len) throws IOException
     
  • readLine() : This method is used to read the line of text.

    public String readLine() throws IOException
     
  • ready() : This method specifies that whether the stream is ready for reading or not.

    public boolean ready() throws IOException
     
  • reset() : This method is used to repositioned the most recent mark.

    public void reset() throws IOException
     
  • skip(long n) : This method is used to skipped over the characters in the stream.

    public long skip(long n) throws IOException
     

Methods of BufferedWriter

  • close() : This method is used to close the stream.

    public void close() throws IOException
     
  • flush() : This method is used to flush the stream.

    public void flush() throws IOException
     
  • newLine() : This method is used to write into new line i.e. used for the line seperator.

    public void newLine() throws IOException
     
  • write(char[] cbuf, int off, int len) : This method is used to write the part of an array of characters.

    public void write(char[] cbuf, int off, int len) throws IOException
     
  • write(int c) : This method is used to write a single character.

    public void write(int c) throws IOException
     
  • write(String s, int off, int len) : This method is used to write a part of string.

    public void write(String s, int off, int len) throws IOException