For example: Gets the file path to C:\Documents and Settings\leeo\my documents\logo.gif
Now want to get the name of the picture logo.gif, we know that the backslash "\" is an escape character, so you cannot directly
String temp[] = filepath.split ("\"), the value of//filepath is the file path above
To split the file path, which should be written
/*
*java \ \ is a \, while the Regex \ \ also means \,
* So when \\\\ resolves to regex, \ \
**/
String temp[] = Filepath.split ("\\\\");
In the Linux system
System.getproperty ("File.separator", "\ \") Output is "/",
And in the Windows system output as "\", so to be compatible with both of these can be written
String temp[] = Filepath.replaceall ("\\\\", "/"). Split ("/");
Get the full code for the file name as follows:
String temp[] = Filepath.replaceall ("\\\\", "/"). Split ("/");
String fileName = ""
if (Temp.length > 1) {
FileName = Temp[temp.length-1];
}
A method of intercepting file names from a path with a backslash in Java