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