BufferedOutputStream in Java


BufferedOutputStream File JavaViews 1756

BufferedOutputStream in Java

BufferedOutputStream is a class in Java which we can use to write data into the output stream. It uses a buffer to write data instead of directly writing into the file. We can use the FileOutputStream class along with the BufferedOutputStream to write information to the file. The performance is faster than other streams since it writes data into the buffer and then copies it into the disk at a time.

Constructors in BufferedOutputStream

Below are the constructors that are part of the Java BufferedOutputStream class.

ConstructorsDescription
BufferedOutputStream(OutputStream out)Creates a new BufferedOutputStream to write data to the output stream
BufferedOutputStream(OutputStream out, int size)Creates a new BufferedOutputStream with the specified buffer size to write data to the output stream

Java BufferedOutputStream Methods

MethodsDescription
void close()Closes the output stream and releases all the resources
void flush()Flushes the BufferedOutputStream and writes all remaining data to the output stream
void write(byte[] b
)
Writes b.length bytes into the output stream
void write(int byte)Writes the specified byte into the output stream
void write(byte[] b, int off, int len)Writes the specified length of bytes from the array to the output stream starting from the offset position.
OutputStream nullOutputStream()Returns a new output stream by discarding all the bytes.

Example: Write specified byte using the BufferedOutputStream

In the below example, we use FileOutputStream along with the BufferedOutputStream to write a single byte of data into the file. The output file will contain the character ‘A’ which is the equivalent value of byte 65.

import java.io.BufferedOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class WriteBufferedFile {

  public static void main(String[] args) throws FileNotFoundException {
    FileOutputStream fo = new FileOutputStream("OutputFile.txt");
    BufferedOutputStream bo = new BufferedOutputStream(fo);
    byte b = 65;
    try {
      bo.write(b);
      System.out.println("The specified byte is successfully written to the output file");
      bo.close();
      fo.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
    

  }

}
The specified byte is successfully written to the output file

Example: Write an array of bytes using BufferedOutputStream

The below example writes an array of bytes as output by converting the specified string into bytes.

import java.io.BufferedOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class WriteBufferedFile {

  public static void main(String[] args) throws FileNotFoundException {
    FileOutputStream fo = new FileOutputStream("OutputFile.txt");
    BufferedOutputStream bo = new BufferedOutputStream(fo);
    String text = "This is an example of BufferedOutputStream";
    try {
      bo.write(text.getBytes());
      System.out.println("An array of bytes is successfully written to the output file");
      bo.close();
      fo.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
    

  }

}
An array of bytes is successfully written to the output file

Example: Write the specific length of bytes using BufferedOutputStream

We can also write a specific length of bytes into an output file using the BufferedOutputStream class in Java. In this example, we write bytes of length 8 starting from position 0. Hence if the input text is “This is an example of BufferedOutputStream”, it writes only “This is “.

import java.io.BufferedOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class WriteBufferedFile {

  public static void main(String[] args) throws FileNotFoundException {
    FileOutputStream fo = new FileOutputStream("OutputFile.txt");
    BufferedOutputStream bo = new BufferedOutputStream(fo);
    String text = "This is an example of BufferedOutputStream";
    try {
      byte[] b = text.getBytes();
      bo.write(b, 0, 8);
      System.out.println("Specific length of bytes is successfully written to the output file");
      bo.close();
      fo.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
    

  }

}
Specific length of bytes is successfully written to the output file

Example: Java BufferedOutputStream flush() method

The flush() method of the BufferedOutputStream class in Java flushes all the remaining bytes and writes it entirely to the output stream. The below example shows how to use the flush() method.

import java.io.BufferedOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class WriteBufferedFile {

  public static void main(String[] args) throws FileNotFoundException {
    FileOutputStream fo = new FileOutputStream("OutputFile.txt");
    BufferedOutputStream bo = new BufferedOutputStream(fo);
    String text = "This is an example of BufferedOutputStream";
    try {
      byte[] b = text.getBytes();
      //Write specific bytes of data
      bo.write(b, 0, 8);
      //Flushes the data
      bo.flush();
      System.out.println("Specific length of bytes is successfully written to the output file");
      //Close the output streams
      bo.close();
      fo.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
    

  }

}
Specific length of bytes is successfully written to the output file

Example: Read a file using BufferedInputStream and write to a file using BufferedOutputStream

The below example shows how to read from a file using the BufferedInputStream and write the same contents to the output file using the BufferedOutputStream class. The input file contains the text “This is an example of BufferedInputStream”.

import java.io.*;

public class ReadAndWrite {

  public static void main(String[] args) throws IOException {
    FileInputStream fi = new FileInputStream("InputFile.txt");
    BufferedInputStream bi = new BufferedInputStream(fi);
    
    FileOutputStream fo = new FileOutputStream("OutputFile.txt");
    BufferedOutputStream bo = new BufferedOutputStream(fo);
    
    char[] text = new char[100];
    
    int a;
    try {
      while((a = bi.read()) != -1) {
        bo.write(a);
      }
      bo.flush();
      
      System.out.println(text);
    } catch (IOException e) {
      e.printStackTrace();
    }
      
    finally {
      if(fi!=null)
        fi.close();
      if(bi!=null)
        bi.close();
      if(fo!=null)
        fo.close();
      if(bo!=null)
        bo.close();
    }
    System.out.println("Content is successfully read and written to file");
  }

}
Content is successfully read and written to file

Input file:

BufferedOutputStream in Java

Output file:

BufferedOutputStream in Java

Reference

Translate »