Java BufferedOutputStream Class
java.io.BufferedOutputStream Class
BufferedOutputStream Class in Java
A BufferedOutputStream is similar to any OutputStream with the exception of an added flush() method that is used to ensure that data buffers are written to the stream being buffered. Since the point of a BufferedOutputStream is to improve performance by reducing the number of times the system actually writes data, you may need to call flush() to cause any data that is in the buffer to be immediately written.
Unlike buffered input, buffering output does not provide additional functionality. Buffers for output in Java are there to increase performance.
In this tutorial you can learn about java io BufferedOutputStream class. And how to use java io BufferedOutputStream class examples.
What is Java BufferedOutputStream Class ?
Java BufferedOutputStream class is used for buffering an output stream.
Java BufferedOutputStream Class Constructors
BufferedOutputStream
public BufferedOutputStream(OutputStream out)Creates a new buffered output stream to write data to the specified underlying output stream.
- Parameters:
out
- the underlying output stream.
BufferedOutputStream
public BufferedOutputStream(OutputStream out, int size)Creates a new buffered output stream to write data to the specified underlying output stream with the specified buffer size.
- Parameters:
out
- the underlying output stream.size
- the buffer size.- Throws:
IllegalArgumentException
- if size <= 0.
java.io.BufferedOutputStream class Example
/* Java BufferedOutputStream class Example Following example shows how to read the text from file using java BufferedOutputStream. Save with file name BufferedOutputStreamExample.java */ import java.io.*; public class BufferedOutputStreamExample { public static void main(String args[]) { // BufferedOutputStream DECLARATION BufferedOutputStream bos = null; // FileOutputStream DECLARATION FileOutputStream fos = null; try { fos = new FileOutputStream("alphabets.txt"); bos = new BufferedOutputStream(fos); // WRITES THE BYTES TO BufferedOutputStream for(int i=0;i<26;i++) { bos.write((byte) ('A'+i)); } } catch(Exception e) { System.out.println("Error Occurred : "+e.getMessage()); } finally { // SHOULD CLOSE STREAMS FINALLY try { if(bos!=null) bos.close(); if(fos!=null) fos.close(); } catch(Exception ex) { System.out.println("Error Occurred : "+ex.getMessage()); } } } }
The above program output may look a little different from what you expected because System.in is line buffered, by default. This means that no input is actually passed to the program until you press ENTER. As you can guess, this does not make read() particularly valuable for interactive console input.
How to read total bytes from file into array
The following program demonstrates BufferedReader and the readLine() method; the program reads and displays lines of text until you enter the word "stop".
/* How to read total bytes from file into array and writes to file using java BufferedOutputStream Example Save with file name BufferedOutputStreamExample2.java */ import java.io.*; public class BufferedOutputStreamExample2 { public static void main(String args[]) { // BufferedInputStream DECLARATION BufferedInputStream bis = null; // FileInputStream DECLARATION FileInputStream fis = null; // ARRAY TO READ BYTES byte bytebuff[] = null; // BufferedOutputStream DECLARATION BufferedOutputStream bos = null; // FileOutputStream DECLARATION FileOutputStream fos = null; try { fis = new FileInputStream("BufferedOutputStreamExample2.java"); bis = new BufferedInputStream(fis); // GET THE AVAILABLE BYTES FROM InputReader AND CREATES ARRAY bytebuff = new byte[fis.available()]; // READ TOTAL BYTES FROM FILE bis.read(bytebuff, 0,fis.available()); fos = new FileOutputStream("duplicatefile.txt"); bos = new BufferedOutputStream(fos); // WRITES THE BYTES TO BufferedOutputStream bos.write(bytebuff,0,bytebuff.length); } catch(Exception e) { System.out.println("Error Occurred : "+e.getMessage()); } finally { // SHOULD CLOSE STREAMS FINALLY try { // RELEASE ARRAY MEMORY bytebuff = null; if(bis!=null) bis.close(); if(fis!=null) fis.close(); if(bos!=null) bos.close(); if(fos!=null) fos.close(); } catch(Exception ex) { System.out.println("Error Occurred : "+ex.getMessage()); } } } }