code: public static File writeToFile(InputStream inputStream, File file) throws IOException {
  
          try (RandomAccessFile raf = new RandomAccessFile(file, "rw")) {
              ReadableByteChannel inputChannel = Channels.newChannel(inputStream);
              FileChannel fileChannel = raf.getChannel();
              fastChannelCopy(inputChannel, fileChannel);
          }
  
          return file;
      }
docs: Writes from an InputStream to a file