We use cookies to collect and analyze information on site performance and usage,
to provide social media features and to enhance and customize content and advertisements.
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
*
*/
}
}