removing invalid characters (("\\/:*?\"<>|") ) from a string to use it as a FileName

You can try this,

String fileName = "\\/:*AAAAA?\"<>|3*7.pdf";
String invalidCharRemoved = fileName.replaceAll("[\\\\/:*?\"<>|]", "");
System.out.println(invalidCharRemoved);

OUTPUT

AAAAA37.pdf

You can use regex

 String s= string.replaceAll("[\\\\/:*?\"<>|]", "");

You should not try to second guess the user. If the provided filename is incorrect just show an error message or throw an exception as appropriate.

Removing those invalid characters from a suplied filename does in no way ensure that the new filename is valid.

Tags:

Java

String