code: public static File ensureWriteableDirectory(File dir)
      throws IOException {
          if (!dir.exists()) {
              boolean success = dir.mkdirs();
              if (!success) {
                  throw new IOException("Failed to create directory: " + dir);
              }
          } else {
              if (!dir.canWrite()) {
                  throw new IOException("Dir " + dir.getAbsolutePath() +
                      " not writeable.");
              } else if (!dir.isDirectory()) {
                  throw new IOException("Dir " + dir.getAbsolutePath() +
                      " is not a directory.");
              }
          }
      
          return dir;
      }
docs: Ensure writeable directory.
  
  If doesn't exist, we attempt creation.
  
  @param dir Directory to test for exitence and is writeable.
  
  @return The passed <code>dir</code>.
  
  @exception IOException If passed directory does not exist and is not
  createable, or directory is not writeable or is not a directory.