Site icon TechVidvan

Java BufferedOutputStream Class with Examples

java buffered outputstream

Java is used in various methods and functions. We are esteemed to learn the BufferedOutputStream in Java. Various examples and explanations are explained.

BufferedOutput is used to write an array of output data.

Explanation of bufferedouputstream in Java

Syntax:

OutputStream os= new BufferedOutputStream(new FileOutputStream)

Java BufferedOutputStream class declaration:

public class BufferedOutputStream extends FilterOutputStream

Working:

To create BufferedOutputStream:

FileOutputStream file = new FileOutputStream(String path);

BufferedOutputStream buffer = new BufferOutputStream(file);

Constructor Table:

              Constructor Description
BufferedOutputStream(OutputStream os) In order to write data to the designated output stream, it generates a new buffered output stream.
BufferedOutputStream(OutputStream os, int size) In order to write data to the specified output stream with the specified buffer size, a new buffered output stream is created.

Class Methods:

Method  Description 
void write(int b) It adds the designated byte to the output stream that has been buffered.
void write(byte[] b, int off, int len) Beginning at the provided offset, it writes the bytes from the supplied byte-input stream into a specified byte array.
void flush() The buffered output stream is flushed.

Write() method:

Flush():

close():

Methods Description
protected byte[] Buf – The data are stored in the internal buffer.
protected int

Example:

import java.io.*;  
public class BufferedOutputStreamExample{    
public static void main(String args[])throws Exception{    
     FileOutputStream fout=new FileOutputStream("D:\\testout.txt");    
     BufferedOutputStream bout=new BufferedOutputStream(fout);    
     String s="Welcome to TechVidvan";    
     byte b[]=s.getBytes();    
     bout.write(b);    
     bout.flush();    
     bout.close();    
     fout.close();    
     System.out.println("Learning Java");    
}    
}

Output:
Learning Java

testout.txt
Welcome to TechVidvan

Example:

public class Main {
    public static void main(String[] args) {

        String data = "TechVidvan Java Course";

        try {
            
            FileOutputStream file = new FileOutputStream(" flush.txt");

                        BufferedOutputStream buffer = new BufferedOutputStream(file);

                        buffer.write(data.getBytes());

            
            buffer.flush();
            System.out.println("Data are stored in TechVidvan");
            buffer.close();
        }

        catch(Exception e) {
            e.getStackTrace();
        }
    }
}

Output:
Data are stored in TechVidvan

Fields:

Difference between buffered and unbuffered output in Java:

Main purposes

Buffer used in JavaStreams:

Difference between BufferedOutput Stream and Output Stream:

Benefits of BufferedOutput Stream:

Conclusion

We have extensively learn the Buffer OutputStream in Java. It give more essential of buffer and data stream usages.Examples are uses for essenting write data in output stream.

Exit mobile version