code: public static FileOutputStream openOutputStream(File file, boolean append) throws IOException {
      if (file.exists()) {
        if (file.isDirectory()) {
          throw new IOException("File '" + file + "' exists but is a directory");
        }
        if (file.canWrite() == false) {
          throw new IOException("File '" + file + "' cannot be written to");
        }
      } else {
        File parent = file.getParentFile();
        if (parent != null) {
          if (!parent.mkdirs() && !parent.isDirectory()) {
            throw new IOException("Directory '" + parent + "' could not be created");
          }
        }
      }
      return new FileOutputStream(file, append);
    }
docs: Opens a {@link FileOutputStream} for the specified file, checking and
  creating the parent directory if it does not exist.
  <p>
  At the end of the method either the stream will be successfully opened,
  or an exception will have been thrown.
  <p>
  The parent directory will be created if it does not exist.
  The file will be created if it does not exist.
  An exception is thrown if the file object exists but is a directory.
  An exception is thrown if the file exists but cannot be written to.
  An exception is thrown if the parent directory cannot be created.
  
  @param file  the file to open for output, must not be <code>null</code>
  @param append if <code>true</code>, then bytes will be added to the
  end of the file rather than overwriting
  @return a new {@link FileOutputStream} for the specified file
  @throws IOException if the file object is a directory
  @throws IOException if the file cannot be written to
  @throws IOException if a parent directory needs creating but that fails
  @since 2.1