Java BufferedOutputStream Tutorial with Examples
1. BufferedOutputStream
No ADS
BufferedOutputStream is a subclass of OutputStream, which is used to simplify writing text to a binary output stream, and improve program performance.
- OutputStream
- ByteArrayOutputStream
- ObjectOutputStream
- PrintStream
- FileOutputStream
- FilterOutputStream
- PipedOutputStream
- DataOutputStream
- InflaterOutputStream
- DigestOutputStream
- DeflaterOutputStream
- CipherOutputStream
- CheckedOutputStream
Operating principle of BufferedOutputStream looks like the following illustration:
BufferedOutputStream wraps inside it an OutputStream object, which is responsible for writing data to the target (such as file).
BufferedOutputStream overrides methods inherited from its parent class, such as write(), write(byte[]),... to ensure that data will be written to buffer and not to target (eg file). But when buffer is full, all of the data in buffer will be pushed into OutputStream and free buffer. You can also call BufferedOutputStream.flush() method to actively push all the data on buffer to OutputStream and clear buffer. Data is also pushed from buffer to OutputStream when calling BufferedOutputStream.close() method.
The so-called "buffer" mentioned above is actually just a byte array. You can specify the size of this array when initializing BufferedOutputStream object.
Data that is temporarily written to buffer minimizes continuous writing to target (eg files on a hard drive), thus it enhances program performance.
BufferedOutputStream constructors
BufferedOutputStream(OutputStream out)
BufferedOutputStream(OutputStream out, int size)
- The BufferedOutputStream(OutputStream,int) constructor creates a BufferedOutputStream object with buffer array of specified size.
- The BufferedOutputStream(OutputStream) constructor creates a BufferedOutputStream object with buffer array of default size (size = 8192).
2. Examples
No ADS
Example: Create a BufferedOutputStream with buffer array size of 16384, which means this buffer is 16384 bytes (16 KB) in size.
File outFile = new File("/Volumes/Data/test/outfile.txt");
outFile.getParentFile().mkdirs(); // Create parent folder.
// Create OutputStream to write a file.
OutputStream os = new FileOutputStream(outFile);
// Create a BufferedOutputStream with buffer array size of 16384 (16384 bytes = 16 KB).
BufferedOutputStream br = new BufferedOutputStream(os, 16384);
Create a BufferedOutputStream with default buffer array size (8192), which is equivalent to 8192 bytes (8 KB).
File outFile = new File("/Volumes/Data/test/outfile.txt");
outFile.getParentFile().mkdirs(); // Create parent folder.
// Create OutputStream to write a file.
OutputStream os = new FileOutputStream(outFile);
// Create a BufferedOutputStream with default buffer array size of 8192 (8192 bytes = 8 KB).
BufferedOutputStream br = new BufferedOutputStream(os);
Full code of example:
BufferedOutputStreamEx1.java
package org.o7planning.bufferedoutputstream.ex;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class BufferedOutputStreamEx1 {
private static final String file_path = "/Volumes/Data/test/outfile.txt";
public static void main(String[] args) throws IOException {
File outFile = new File(file_path);
outFile.getParentFile().mkdirs(); // Create parent folder.
// Create OutputStream to write a file.
OutputStream os = new FileOutputStream(outFile);
// Create a BufferedOutputStream with buffer array size of 16384 (16384 bytes = 16 KB).
BufferedOutputStream br = new BufferedOutputStream(os, 16384);
br.write("JP".getBytes("UTF-8"));
br.write('\n');
br.write("日本".getBytes("UTF-8"));
br.write('\n');
br.flush();
br.write('\n');
br.write("八洲".getBytes("UTF-8"));
br.close();
}
}
Output:
outfile.txt
JP
日本
八洲
No ADS
Java IO Tutorials
- Java CharArrayWriter Tutorial with Examples
- Java FilterReader Tutorial with Examples
- Java FilterWriter Tutorial with Examples
- Java PrintStream Tutorial with Examples
- Java BufferedReader Tutorial with Examples
- Java BufferedWriter Tutorial with Examples
- Java StringReader Tutorial with Examples
- Java StringWriter Tutorial with Examples
- Java PipedReader Tutorial with Examples
- Java LineNumberReader Tutorial with Examples
- Java PrintWriter Tutorial with Examples
- Java IO Binary Streams Tutorial with Examples
- Java IO Character Streams Tutorial with Examples
- Java BufferedOutputStream Tutorial with Examples
- Java ByteArrayOutputStream Tutorial with Examples
- Java DataOutputStream Tutorial with Examples
- Java PipedInputStream Tutorial with Examples
- Java OutputStream Tutorial with Examples
- Java ObjectOutputStream Tutorial with Examples
- Java PushbackInputStream Tutorial with Examples
- Java SequenceInputStream Tutorial with Examples
- Java BufferedInputStream Tutorial with Examples
- Java Reader Tutorial with Examples
- Java Writer Tutorial with Examples
- Java FileReader Tutorial with Examples
- Java FileWriter Tutorial with Examples
- Java CharArrayReader Tutorial with Examples
- Java ByteArrayInputStream Tutorial with Examples
- Java DataInputStream Tutorial with Examples
- Java ObjectInputStream Tutorial with Examples
- Java InputStreamReader Tutorial with Examples
- Java OutputStreamWriter Tutorial with Examples
- Java InputStream Tutorial with Examples
- Java FileInputStream Tutorial with Examples
Show More