如何将一个流读入另一个流? [英] How to read one stream into another?

查看:122
本文介绍了如何将一个流读入另一个流?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

FileInputStream in = new FileInputStream(myFile);
ByteArrayOutputStream out = new ByteArrayOutputStream();

问题:如何在中的所有内容>进入 out 的方式不是用我自己的字节缓冲区手工制作的循环?

Question: How can I read everything from in into out in a way which is not a hand-crafted loop with my own byte buffer?

推荐答案

编写一个方法来执行此操作,并从需要该功能的所有地方调用它。 Guava 已经在 ByteStreams.copy 。我确信任何其他具有通用IO功能的库也有它,但是Guava是我的第一个首选库。摇滚:))

Write one method to do this, and call it from everywhere which needs the functionality. Guava already has code for this, in ByteStreams.copy. I'm sure just about any other library with "general" IO functionality has it too, but Guava's my first "go-to" library where possible. It rocks :)

这篇关于如何将一个流读入另一个流?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆