How to use inputstream and outputstream in java

Posted on 14.06.2018 by Admin
If you have some big amount of data, then you want to do the conversion in realtime in form of stream where whole data is not stored in buffer at any amount of time. Do you know how to convert OutputStream to InputStream not vice versa, I mean really this way tha. Io package, but its become very simple once you know there is a class called ByteArrayInputStream.
By the way, if you are not using try-with-resource statement, then don't forget to close Streams once you are through with it. I wanted to get know it with an OutputStream itself. Don't worry Java has solution for that, You can use ByteArrayInputStream to convert byte. I am on the stage of development, where I have two modules and from one I got output as a OutputStream and second one, which accepts only InputStream. Both of this class have the corresponding method to write primitive data type and to read it back. It might sound difficult initially, because of limited knowledge of java. DataInputStream give us the power to write and read primitive data type to a media such as file.

This article titles Convert OutputStream to InputStream using byte array, but in fact what is being shown is how to convert a FileOutputStream a subtype of OutputStream.

I do not like the idea of copying. I was surprised to find today that I couldn't track down any simple way to write the contents of an InputStream to an OutputStream in Java. Next part of this example shows, how can you write a byte array to an OutputStream. Since its a child class of InputStream, you can pass it around in place of InputStream. It can be argued that, whether a method, which accepts an InputStream should close it or not, but I have closed it to be on safe side. In other words, you will need to create a pipe approach where data is flowing from one end to other end and no need to store complete data in buffer.