Java I/O Buffered Streams

Buffered Streams Overview

In unbuffered I/O, each read or writes request is handled directly by the underlying OS. This can make a program much less efficient since each such request often triggers disk access, network activity, or some other operation that is relatively expensive.
To reduce this kind of overhead, the Java platform implements buffered I/O streams. Buffered input streams read data from a memory area known as a buffer; the native input API is called only when the buffer is empty. Similarly, buffered output streams write data to a buffer, and the native output API is called only when the buffer is full.
There are four buffered stream classes used to wrap unbuffered streams:
Let's demonstrate Buffered Streams I/O classes with examples.

BufferedOutputStream Class

Java BufferedOutputStream class is used for buffering an output stream. It internally uses a buffer to store data. It adds more efficiency than to write data directly into a stream. So, it makes the performance fast.

BufferedOutputStream Class Example

package com.javaguides.javaio.fileoperations.examples;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class BufferedOutputStreamExample {
 public static void main(String[] args) {
  File file = new File("C:/Project_Work/workspace/java-io-guide/sample.txt");
  String content = "This is the text content";

  try (OutputStream out = new FileOutputStream(file);
    BufferedOutputStream bout = new BufferedOutputStream(out);) {

   // if file doesn't exists, then create it
   if (!file.exists()) {
    file.createNewFile();
   }
   // get the content in bytes
   bout.write(content.getBytes());
  } catch (IOException e) {
   e.printStackTrace();
  }
 }
}

BufferedInputStream Class

Java BufferedInputStream class is used to read information from the stream. It internally uses the buffer mechanism to make the performance fast.
The important points about BufferedInputStream are:
  • When the bytes from the stream are skipped or read, the internal buffer automatically refilled from the contained input stream, many bytes at a time.
  • When a BufferedInputStream is created, an internal buffer array is created.

BufferedInputStream Example

package com.javaguides.javaio.fileoperations.examples;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;

public class BufferedInputStreamExample {
 public static void main(String[] args) {
  try( FileInputStream fin=new FileInputStream("D:\\testout.txt");    
       BufferedInputStream bin=new BufferedInputStream(fin); ){
   int i;    
      while((i=bin.read())!=-1){    
       System.out.print((char)i);    
      }    
  } catch (IOException e) {
   e.printStackTrace();
  }
 }
}

BufferedWriter Class

Java BufferedWriter class is used to provide buffering for Writer instances. It makes the performance fast. It inherits Writer class. The buffering characters are used for providing the efficient writing of single arrays, characters, and strings.

BufferedWriter Class Example

Let's see the simple example of writing the data to a text file sample.txt using Java BufferedWriter.
package com.javaguides.javaio.fileoperations.examples;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

public class BufferedWriterExample {
 public static void main(String[] args) {
  try (FileWriter writer = new FileWriter("D:\\sample.txt");
   BufferedWriter buffer = new BufferedWriter(writer);) {
   buffer.write("Welcome to JavaGuides.");
   System.out.println("Success");
  } catch (IOException e) {
   e.printStackTrace();
  }

 }
}

BufferedReader Class

Java BufferedReader class is used to read the text from a character-based input stream. It can be used to read data line by line by readLine() method. It makes the performance fast. It inherits Reader class.

BufferedReader Class Example

package com.javaguides.javaio.fileoperations.examples;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class BufferedReaderExample {
 public static void main(String[] args) {
  try (FileReader fr = new FileReader("C:/Project_Work/sample.txt"); 
    BufferedReader br = new BufferedReader(fr);) {
   int i;
   while ((i = br.read()) != -1) {
    System.out.print((char) i);
   }
  } catch (IOException e) {
   e.printStackTrace();
  }
 }
}

Learn complete Java I/O on : Java I/O Guide

Comments