public static void checkDirectoryIsWriteable(@NotNull File directory) throws IOException {
  while (!directory.exists() || !directory.isDirectory()) {
    directory=directory.getParentFile();
  }
  if (!directory.canWrite()) {
    throw new IOException("Cannot write to folder: " + directory.getAbsolutePath());
  }
}

public static void checkDirectoryIsWriteable ( @ NotNull File directory ) throws IOException { while ( ! directory . exists ( ) || ! directory . isDirectory ( ) ) { directory = directory . getParentFile ( ) ; } if ( ! directory . canWrite ( ) ) { throw new IOException ( "Cannot write to folder: " + directory . getAbsolutePath ( ) ) ; } }
find the first parent directory that exists and check if this directory is writeable .
