176

Is there any way to create a java.io.File object from an java.io.InputStream ?

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.

Gautam
  • 7,868
  • 12
  • 64
  • 105
androidgalaxyman
  • 1,797
  • 2
  • 11
  • 10
  • 1
    my requirement is reading the File from a RAR . Assume i am not try to write the temporary File from the RAR, inside RAR i am having a File, just i need to read. – androidgalaxyman Jul 16 '12 at 10:24
  • I don't understand the question. Please clarify. If you want the oriinal File from the InputStream, there wasn't one: you are reading a RAR file, not a File. If you want something else, what? – user207421 Jul 16 '12 at 10:40
  • 1
    Possible duplicate of [How to convert InputStream to virtual File](https://stackoverflow.com/questions/4317035/how-to-convert-inputstream-to-virtual-file) – Paulo Oliveira Jun 14 '17 at 09:18

8 Answers8

125

You need to create new file and copy contents from InputStream to that file:

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

I am using convenient IOUtils.copy() to avoid manual copying of streams. Also it has built-in buffering.

Abdelghani Roussi
  • 2,707
  • 2
  • 21
  • 39
Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674
94

In one line :

FileUtils.copyInputStreamToFile(inputStream, file);

(org.apache.commons.io)

Victor Petit
  • 2,632
  • 19
  • 19
55

Since Java 7, you can do it in one line even without using any external libraries:

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

See the API docs.

kidney
  • 2,663
  • 17
  • 23
40

Create a temp file first using org.apache.commons.io.

File tempFile = File.createTempFile(prefix, suffix);
tempFile.deleteOnExit();
FileOutputStream out = new FileOutputStream(tempFile);
IOUtils.copy(in, out);
return tempFile;
Shehan Simen
  • 1,046
  • 1
  • 17
  • 28
13

Easy Java 9 solution with try with resources block

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

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

}

java.io.InputStream#transferTo is available since Java 9.

MariuszS
  • 30,646
  • 12
  • 114
  • 155
10

If you do not want to use other libraries, here is a simple function to copy data from an InputStream to an 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);
    }
}

Now, you can easily write an Inputstream into a file by using FileOutputStream-

FileOutputStream out = new FileOutputStream(outFile);
copyStream (inputStream, out);
out.close();
Nabin Bhandari
  • 15,949
  • 6
  • 45
  • 59
1

If you are using Java version 7 or higher, you can use try-with-resources to properly close the FileOutputStream. The following code use IOUtils.copy() from commons-io.

public void copyToFile(InputStream inputStream, File file) throws IOException {
    try(OutputStream outputStream = new FileOutputStream(file)) {
        IOUtils.copy(inputStream, outputStream);
    }
}  
h3xStream
  • 6,293
  • 2
  • 47
  • 57
0

I know this question is for Java but this comes up big on Google for Android searches as well, and a lot of modern Android uses Kotlin. So here's how easy it is in Kotlin, no 3rd party libs needed at all: inputstream.copyTo(os)

A more fleshed out example:

// 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)

And now your file is /data/data/com.your.packagename/cache/output.jpg. And if you're wondering based on the SO question title, you can't create a File object from an InputStream without creating a real file somewhere, you can't write to a virtual file or a File object in memory or something like that.

georgiecasey
  • 21,793
  • 11
  • 65
  • 74