Java BufferedOutputStream class is used for buffering an output stream. It internally uses a buffer to store data. It adds more efficiency than to write data directly into a stream. So, it makes the performance fast.
BufferedOutputStream Class Example
This program uses text content as input and writes to file named "sample.txt".
String content = "This is the text content";
Note: This program uses try-with-resources. It requires JDK 7 or later.
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
/**
* The class demonstrate the usage of BufferedOutputStream class methods.
* @author javaguides.net
*
*/
public class BufferedOutputStreamExample {
public static void main(String[] args) {
File file = new File("sample.txt");
String content = "This is the text content";
try (OutputStream out = new FileOutputStream(file);
BufferedOutputStream bout = new BufferedOutputStream(out);) {
// if file doesn't exists, then create it
if (!file.exists()) {
file.createNewFile();
}
// get the content in bytes
bout.write(content.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
}
}
Reference
https://www.javaguides.net/2018/08/bufferedoutputstream-class-in-java.htmlFree Spring Boot Tutorial - 5 Hours Full Course
Watch this course on YouTube at Spring Boot Tutorial | Fee 5 Hours Full Course