java 将 ByteArrayOutputStream 转换为 FileInputStream

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17595891/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 02:25:29  来源:igfitidea点击:

convert ByteArrayOutputStream to FileInputStream

java

提问by Uwe Plonus

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

我已将流保存在ByteArrayOutputStream. 现在我想在FileInputStream. 我怎样才能做到这一点?

it's my outputStream.

这是我的输出流。

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

now how to read that, from FileInputStream?

现在如何阅读,从FileInputStream

回答by Uwe Plonus

You can create a ByteArrayInputStreamwith

您可以创建一个ByteArrayInputStream

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

and then read from this InputStream.

然后从这里开始阅读InputStream

If your interface only accepts a FileInputStreamthen the interface is broken...

如果您的接口只接受 aFileInputStream那么接口坏了...

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

如果接口只适用于文件,它应该接受File其他文件,否则它应该使用InputStream.

Also if you use threads you can use PipedInputStreamand PipedOutputStreamdirectly between the threads.

此外,如果您使用线程,则可以直接在线程之间使用PipedInputStreamPipedOutputStream