Replacing illegal character in fileName
You need to replace everything but [a-zA-Z0-9.-]
.
The ^
within the brackets stands for "NOT".
myString = myString.replaceAll("[^a-zA-Z0-9\\.\\-]", "_");
If you are looking for options on windows platform then you can try below solution to make use of all valid characters other than "\/:*?"<>|" in file name.
fileName = fileName.replaceAll("[\\\\/:*?\"<>|]", "_");