能否从InputStream创建一个File对象?

178

有没有办法从 java.io.InputStream 创建一个 java.io.File 对象?

我的需求是从 RAR 文件中读取文件。我不想写一个临时文件,我要读取压缩包中的文件。


1
我的需求是从RAR文件中读取文件。假设我不尝试从RAR中写入临时文件,我只需要读取RAR中的文件。 - androidgalaxyman
我不明白这个问题,请澄清一下。如果你想要从InputStream中获取原始文件,那么是没有的:你正在读取一个RAR文件,而不是一个File。如果你想要其他东西,那是什么? - user207421
1
可能是如何将InputStream转换为虚拟文件的重复问题。 - Paulo Oliveira
8个回答

127

你需要创建一个新文件,并将InputStream的内容复制到该文件中:

File file = //...
try(OutputStream outputStream = new FileOutputStream(file)){
    IOUtils.copy(inputStream, outputStream);
} catch (FileNotFoundException e) {
    // handle exception here
} catch (IOException e) {
    // handle exception here
}

我正在使用方便的IOUtils.copy()来避免手动复制流。它还具有内置缓冲。


3
在Android Studio中无法解决IOUtils问题,你引用的链接是404错误。 - Shahraiz T.
3
需要添加org.apache.directory.studio:org.apache.commons.io作为依赖项。 是我的错误 :) - Shahraiz T.
15
请正确关闭OutputStream,可以使用try-with-resources或IOUtils.closeQuietly函数。我知道这只是一个例子,但初学者可能会照字面意思复制它。 - Rafael Membrives
5
有人能解释一下 "File file = //..." 吗?我该如何先初始化这个文件? - Alex Link

93
在一行中:
FileUtils.copyInputStreamToFile(inputStream, file);

(org.apache.commons.io)


57

自Java 7以来,即使不使用任何外部库,您也可以在一行中完成它:

Files.copy(inputStream, outputPath, StandardCopyOption.REPLACE_EXISTING);

请查看API文档


12
如果您要在Android上使用此功能,需要最低SDK版本为26。 - Neph

40

首先使用org.apache.commons.io创建一个临时文件。

File tempFile = File.createTempFile(prefix, suffix);
tempFile.deleteOnExit();
FileOutputStream out = new FileOutputStream(tempFile);
IOUtils.copy(in, out);
return tempFile;

请在代码中包含IOUtils的包名。 - Ridhuvarshan
1
org.apache.commons.io - Shehan Simen
它也存在于org.apache.axis2.util中,具有复制函数的第三个参数。该功能在那里也可以工作。对于使用什么进行比较? - Ridhuvarshan

13

使用 try with resources 块的简单 Java 9 解决方案

public static void copyInputStreamToFile(InputStream input, File file) {  

    try (OutputStream output = new FileOutputStream(file)) {
        input.transferTo(output);
    } catch (IOException ioException) {
        ioException.printStackTrace();
    }

}

Java 9提供了 java.io.InputStream#transferTo 方法。


1
它的哪个部分需要Java 11? - MauriceNino
1
@MauriceNino 不好意思,感谢您的评论,“try-with-resources - 在Java 7中引入”。 - MariuszS

11

如果您不想使用其他库,这里有一个简单的函数可以将数据从InputStream复制到OutputStream

public static void copyStream(InputStream in, OutputStream out) throws IOException {
    byte[] buffer = new byte[1024];
    int read;
    while ((read = in.read(buffer)) != -1) {
        out.write(buffer, 0, read);
    }
}

现在,您可以通过使用FileOutputStream轻松将Inputstream写入文件-

FileOutputStream out = new FileOutputStream(outFile);
copyStream (inputStream, out);
out.close();

1
如果您使用Java 7或更高版本,则可以使用try-with-resources正确关闭FileOutputStream。以下代码使用commons-io中的IOUtils.copy()
public void copyToFile(InputStream inputStream, File file) throws IOException {
    try(OutputStream outputStream = new FileOutputStream(file)) {
        IOUtils.copy(inputStream, outputStream);
    }
}  

你不需要关闭OutputStream吗?就像Tomasz N.的答案中所说的那样。 - trinity420
5
try初始化部分指定资源时,这些资源会在退出try块时自动关闭。 - h3xStream
这适用于安卓系统吗? - gumuruh
@gumuruh 是的。https://dev59.com/wGAf5IYBdhLWcg3wwlC5#24290875 - h3xStream

0

我知道这个问题是关于Java的,但在Android搜索中,它也会出现在Google上,并且许多现代Android使用Kotlin。因此,在Kotlin中实现非常容易,完全不需要第三方库:inputstream.copyTo(os)

以下是更详细的示例:

// I'm loading my InputStream from resources, you can get it anywhere
val inputstream = getResources().openRawResource(R.raw.ronan_collins)
// getCacheDir is /data/data/com.your.packagename/cache if you forgot. 
// Which is probably the best place on Android to create tmp files
val outputFile = File(cacheDir, "output.jpg")
val os = FileOutputStream(outputFile)
inputstream.copyTo(os)

现在你的文件位于 /data/data/com.your.packagename/cache/output.jpg。 如果你根据 SO 问题标题想知道,你不能从 InputStream 创建一个 File 对象而不在某个地方创建一个真正的文件,你不能写入虚拟文件或内存中的 File 对象或类似的东西。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接