是否可以从InputStream创建File对象 [英] Is it possible to create a File object from InputStream
本文介绍了是否可以从InputStream创建File对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有办法从 java.io.InputStream
创建 java.io.File
对象?
Is there any way to create a java.io.File
object from an java.io.InputStream
?
我的要求是从RAR读取文件。我不是要写一个临时文件,我在RAR档案中有一个文件,我正在尝试阅读。
My requirement is reading the File from a RAR . I am not trying to write a temporary File, I have a file inside RAR archive which I am trying to read.
推荐答案
你需要创建新文件并将内容从 InputStream
复制到该文件:
You need to create new file and copy contents from InputStream
to that file:
File file = //...
OutputStream outputStream = new FileOutputStream(file);
IOUtils.copy(inputStream, outputStream);
outputStream.close();
我使用方便 IOUtils.copy()
避免手动复制流。它还有内置缓冲。
I am using convenient IOUtils.copy()
to avoid manual copying of streams. Also it has built-in buffering.
这篇关于是否可以从InputStream创建File对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文