Is it possible to create a File object from InputStream

JavaIo

Java Problem Overview


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.

Java Solutions


Solution 1 - Java

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.

Solution 2 - Java

In one line :

FileUtils.copyInputStreamToFile(inputStream, file);

(org.apache.commons.io)

Solution 3 - Java

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.

Solution 4 - Java

Create a temp file first.

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

Solution 5 - Java

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();

Solution 6 - Java

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.

Solution 7 - Java

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);
    }
}  

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionandroidgalaxymanView Question on Stackoverflow
Solution 1 - JavaTomasz NurkiewiczView Answer on Stackoverflow
Solution 2 - JavaVictor PetitView Answer on Stackoverflow
Solution 3 - JavakidneyView Answer on Stackoverflow
Solution 4 - JavaShehan SimenView Answer on Stackoverflow
Solution 5 - JavaNabin BhandariView Answer on Stackoverflow
Solution 6 - JavaMariuszSView Answer on Stackoverflow
Solution 7 - Javah3xStreamView Answer on Stackoverflow