code: public static String getSafeFilename(String filename)
     {
        String result = filename;
        if (result != null)
        {
           result = result.replaceAll("[^a-zA-Z0-9]+", "-").replaceAll("^-+", "");
        }
        return result;
     }
docs: Generate a string that is usable as a single file name or directory path segment on any operating system. Replaces
  unsafe characters with the underscore '_' character