Is it possible to create a File object from InputStream

In one line :

FileUtils.copyInputStreamToFile(inputStream, file);

(org.apache.commons.io)


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.

Tags:

Java

Io