Convert OutputStream into BufferedOutputStream in Java using Apache Commons IO
Tags: Convert IOUtils Apache Commons Apache Commons IO OutputStream FileOutputStream BufferedOutputStream
In this Java tutorial we learn how to use the IOUtils utility class in Apache Commons IO to convert an OutputStream object into a BufferedOutputStream object in Java.
How to add Apache Commons IO library to your Java project
To use the Apache Commons IO library in the Gradle build project, add the following dependency into the build.gradle file.
implementation 'commons-io:commons-io:2.8.0'
To use the Apache Commons IO library in the Maven build project, add the following dependency into the pom.xml file.
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.8.0</version>
</dependency>
To have more information about the Apache Commons IO library you can visit the library home page at commons.apache.org/proper/commons-io/
How to convert OutputStream into BufferedOutputStream
The Java application below shows you how to convert an OutputStream object into a BufferedOutputStream object.
ConvertToBufferedOutputStream.java
import org.apache.commons.io.IOUtils;
import java.io.BufferedOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class ConvertToBufferedOutputStream {
public static void main(String... args) {
try {
OutputStream outputStream = new FileOutputStream("D:\\Data\\data.txt");
BufferedOutputStream bufferedOutputStream = IOUtils.buffer(outputStream);
bufferedOutputStream.write("simplesolution.dev".getBytes());
IOUtils.close(bufferedOutputStream);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
After executing the Java program above, a new file was created at D:\Data\data.txt with the content as below.
D:\Data\data.txt
simplesolution.dev
Happy Coding 😊