public static String toFileSystemSafeName(String name,boolean dirSeparators,int maxFileLength){
  int size=name.length();
  StringBuilder rc=new StringBuilder(size * 2);
  for (int i=0; i < size; i++) {
    char c=name.charAt(i);
    boolean valid=c >= 'a' && c <= 'z';
    valid=valid || (c >= 'A' && c <= 'Z');
    valid=valid || (c >= '0' && c <= '9');
    valid=valid || (c == '_') || (c == '-')|| (c == '.');
    valid=valid || (dirSeparators && ((c == '/') || (c == '\\')));
    if (valid) {
      rc.append(c);
    }
  }
  String result=rc.toString();
  if (result.length() > maxFileLength) {
    result=result.substring(result.length() - maxFileLength,result.length());
  }
  return result;
}

public static String toFileSystemSafeName ( String name , boolean dirSeparators , int maxFileLength ) { int size = name . length ( ) ; StringBuilder rc = new StringBuilder ( size * 2 ) ; for ( int i = 0 ; i < size ; i ++ ) { char c = name . charAt ( i ) ; boolean valid = c >= 'a' && c <= 'z' ; valid = valid || ( c >= 'A' && c <= 'Z' ) ; valid = valid || ( c >= '0' && c <= '9' ) ; valid = valid || ( c == '_' ) || ( c == '-' ) || ( c == '.' ) ; valid = valid || ( dirSeparators && ( ( c == '/' ) || ( c == '\\' ) ) ) ; if ( valid ) { rc . append ( c ) ; } } String result = rc . toString ( ) ; if ( result . length ( ) > maxFileLength ) { result = result . substring ( result . length ( ) - maxFileLength , result . length ( ) ) ; } return result ; }
converts any string into a string that is safe to use as a file name . the result will only include ascii characters and numbers , and the " - " , " _ " , and " . " characters .
