code: public static void setFileContents(File file, byte[] contents, boolean createDirectory) throws IOException {
      if (createDirectory) {
        File directory = file.getParentFile();
        if (!directory.exists()) {
          directory.mkdirs();
        }
      }
  
      FileOutputStream stream = new FileOutputStream(file);
      stream.write(contents);
      stream.close();
    }
docs: Writes the specified byte array to a file.
  @param file The <code>File</code> to write.
  @param contents The byte array to write to the file.
  @param createDirectory A value indicating whether the directory
  containing the file to be written should be created if it does
  not exist.
  @throws IOException If the file could not be written.