Force a valid Windows filenameTag(s): IO


public class FileUtils {

 /**
  * replace illegal characters in a filename with "_"
  * illegal characters :
  *           : \ / * ? | < >
  * @param name
  * @return
  */
  public static String sanitizeFilename(String name) {
    return name.replaceAll("[:\\\\/*?|<>]", "_");
  }

  public static void main(String args[]) throws Exception {

    String test = "invalid : file ? name.txt";
    System.out.println(test + " -> " + FileUtils.sanitizeFilename(test));
    /* output :
     *
     *   invalid : file ? name. -> invalid _ file _ name.txt
     *
     */
  }
}
Related HowTo : Check if filename is valid
blog comments powered by Disqus