I have a complete file path and I want to get the file name.
I am using the following instruction:
String[] splittedFileName = fileName.split(System.getProperty("file.separator"));
String simpleFileName = splittedFileName[splittedFileName.length-1];
But on Windows it gives:
java.util.regex.PatternSyntaxException: Unexpected internal error near index 1
\
^
Can I avoid this exception? Is there a better way to do this?
The problem is that
\has to be escaped in order to use it as backslash within a regular expression. You should either use a splitting API which doesn’t use regular expressions, or usePattern.quotefirst:Or even better, use the
FileAPI for this: