BufferedOutputStream in Java

BufferedOutputStream extends FilterOutputStream.

BufferedOutputStream implements a buffered output stream. By setting up such an output stream, an application can write bytes to the underlying output stream without necessarily causing a call to the underlying system for each byte written.

Constructors

BufferedOutputStream in Java

Methods

Screen Shot 2020-03-31 at 6.44.25 PM

package com.java.io;

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

public class BufferedOutputStreamExample {

    public static void main(String[] args) {

        FileOutputStream fos = null;
        BufferedOutputStream bos = null;
        String data = "Testing data";

        try {
            fos = new FileOutputStream("/Test/TestFolder/test-file3.txt");
            bos = new BufferedOutputStream(fos);
            byte[] bytesArray = data.getBytes();
            bos.write(bytesArray);
            bos.flush();
            System.out.println("Data written to File");
        } catch (IOException ioe) {
            ioe.printStackTrace();
        } finally {
            // Closing the streams
            if (fos != null)
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            if (bos != null)
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
        }
    }

}
Data written to File

File

Screen Shot 2020-03-31 at 6.39.56 PM

Related Article

Byte Stream Hierarchy in Java

Leave a Reply

Your email address will not be published. Required fields are marked *