How to Convert InputStream to OutputStream in Java
In this tutorial, we will see how to convert InputStream to OutputStream in Java.
Table of Content :
- What is an OutputStream?
- What is an InputStream?
- Difference between OutputStream and InputStream
- InputStream to OutputStream in Java
- Using InputStream.transferTo() Method of Java 9
- Using java 8
- Using Java 7 Files
- Using Google's Guava
- Using Apache Common IO
- Using Spring Framework utility classes
- Summary
What is an OutputStream?
An OutputStream is one where you write data and someone reads it at the other end.
What is an InputStream?
InputStream class, java.io.InputStream, represents an ordered stream of bytes. Means can read data from a Java InputStream as an ordered sequence of bytes. This is useful when reading data from a file, or received over the network.
Difference between OutputStream and InputStream
OutputStream | InputStream |
---|---|
OutputStream Write Data to the destination once at a time. | InputStream Read data from the source once at a time. |
It is an abstract class that describes Stream Output. | It is an abstract class that describes Stream Input. |
Types of OutputStream are: FileOutputStream, ByteArrayOutputStream, FilterOutputStream and ObjectOutputStream. | Types of InputStream are: FileInputStream ByteArrayInputStream FilterInputStream and ObjectInputStream. |
The most important and mostly used type is FileInputStream. | The most important and mostly used type is FileInputStream. |
Firstly we will how it will be done using the core java and after that, we will see it using third-party libraries like Apache Common IO and Google's Guava.
1). Using InputStream.transferTo() Method of Java 9
We can use InputStream.transferTo() method to copy data from InputStream to OutputStream. This method reads all bytes from this input stream and writes the bytes to the given output stream in the original order.
try (InputStream in =Files.newInputStream(Paths.get("hello.txt"));
OutputStream out = Files.newOutputStream(Paths.get("welcome.txt"))) {
long length = in.transferTo(out);
System.out.println("Bytes transferred: " + length);
} catch(IOException ex) {
System.out.println("Error occured during the conversion " + ex);
}
2). Using java 8
In java 8 or lower version of JDK,we have to do conversion given below.
try (InputStream inputStream = Files.newInputStream(Paths.get("hello.txt"));
OutputStream outputStream = Files.newOutputStream(Paths.get("world.txt"))) {
int length;
byte[] bytes = new byte[1024];
while ((length = inputStream.read(bytes)) != -1) {
outputStream.write(bytes, 0, length);
}
} catch(IOException ex) {
System.out.println("IOException occured during the conversion " + ex);
}
3). Using Java 7 Files
Files.copy(InputStream inputStream, Path target)
Files.copy(Path source, OutputStream outputStream)
To write into an existing file (e.g. one created with File.createTempFile()), you need to pass the REPLACE_EXISTING copy option, else FileAlreadyExistsException is thrown.
Files.copy(in, target, StandardCopyOption.REPLACE_EXISTING)
4). Using Google's Guava
Here we convert Inputstream to Outputstream by using ByteStreams class.
We need maven dependency
<dependency>
<groupid>com.google.guava</groupid>
<artifactid>guava</artifactid>
<version>29.0-jre</version>
</dependency>
String stringData = "Welcome to java World!!!";
try (InputStream inputStream = new ByteArrayInputStream(stringData.getBytes());
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream()) {
ByteStreams.copy(inputStream, byteArrayOutputStream);
}
5). Using Apache Common IO
The Apache Commons IO has IOUtils class contains copy method, which converts InputStram to OutputStream
We need maven dependency
<!--https://mvnrepository.com/artifact/commons-io/commons-io-->
<dependency>
<groupid>commons-io</groupid>
<artifactid>commons-io</artifactid>
<version>2.8.0</version>
</dependency>
try (InputStream inputStream = Files.newInputStream(Paths.get("input.txt"));
OutputStream outputStream = Files.newOutputStream(Paths.get("output.txt"))) {
int bytesCopied = IOUtils.copy(inputStream, outputStream);
System.out.println("inputStream to outputStream conversion completed...");
} catch (IOException ex) {
System.out.println("IOException occured during the conversion " + ex);
}
6). Using Spring Framework utility classes
a). FileCopyUtils
Simple utility methods for file and stream copying. All copy methods use a block size of 4096 bytes and close all affected streams when done. A variation of the copy methods from this class that leave streams open can be found in StreamUtils.
FileCopyUtils.copy(inputStream, outputStream);
b). StreamUtils
Simple utility methods for dealing with streams. The copy methods of this class are similar to those defined in FileCopyUtils except that all affected streams are left open when done. All copy methods use a block size of 4096 bytes.
StreamUtils.copy(inputStream, outputStream);
Post/Questions related to Convert InputStream to OutputStream in Java
Read file to byte[] array in Java
How to convert the input stream to byte array in java?
Create New File in Java
How to convert byte array to input stream in java?
Which of the below stream is used to write data to any destination byte by byte?
How do you create an InputStream from a byte array?
How to Convert OutputStream to InputStream in Java
In this article, we have seen What is an InputStream,OutputStream and difference between InputStream and OutputStream We have seen the various ways to convert InputStream to OutputStream in Java.
0 Comments
Post a Comment