7

I have stream saved in ByteArrayOutputStream. now I want to to read that in FileInputStream. how Can I do that?

it's my outputStream.

... OutputStream out = new ByteArrayOutputStream(); ... 

now how to read that, from FileInputStream?

4
  • 2
    outputStreams are meant for writing, you can read from them Commented Jul 11, 2013 at 14:20
  • close the outputstream, and open the file with the fileinputstream? Commented Jul 11, 2013 at 14:21
  • Stream is in my memory - it's ByteArrayOutputStream. then I need FileInputStream variable, of that Stream. Commented Jul 11, 2013 at 14:22
  • I don't have file. I have ByteArrayOutputStream. Commented Jul 11, 2013 at 14:23

1 Answer 1

16

You can create a ByteArrayInputStream with

InputStream is = new ByteArrayInputStream(bos.toByteArray()); 

and then read from this InputStream.

If your interface only accepts a FileInputStream then the interface is broken...

If, at all, an interface only works with files it should accept a File else it should use an InputStream.

Also if you use threads you can use PipedInputStream and PipedOutputStream directly between the threads.

Sign up to request clarification or add additional context in comments.

2 Comments

I need only FileInputStream. or I have byte array (byte [] variable) and I want to read from FileInputStream
+1 for "If your interface only accepts a FileInputStream then the interface is broken"