Get file name from a file location in Java
Solution 1
new File(fileName).getName();
or
int idx = fileName.replaceAll("\\\\", "/").lastIndexOf("/");
return idx >= 0 ? fileName.substring(idx + 1) : fileName;
Notice that the first solution is system dependent. It only takes the system's path separator character into account. So if your code runs on a Unix system and receives a Windows path, it won't work. This is the case when processing file uploads being sent by Internet Explorer.
Solution 2
new File(absolutePath).getName();
Solution 3
Apache Commons IO provides the FilenameUtils class which gives you a pretty rich set of utility functions for easily obtaining the various components of filenames, although The java.io.File class provides the basics.
Solution 4
From Apache Commons IO FileNameUtils
String fileName = FilenameUtils.getName(stringNameWithPath);
Solution 5
Here are 2 ways(both are OS independent.)
Using Paths
: Since 1.7
Path p = Paths.get(<Absolute Path of Linux/Windows system>);
String fileName = p.getFileName().toString();
String directory = p.getParent().toString();
Using FilenameUtils
in Apache Commons IO :
String name1 = FilenameUtils.getName("/ab/cd/xyz.txt");
String name2 = FilenameUtils.getName("c:\\ab\\cd\\xyz.txt");
Ankur
A junior BA have some experience in the financial services industry. I do programming for my own personal projects hence the questions might sound trivial.
Updated on August 02, 2020Comments
-
Ankur almost 4 years
I have a
String
that provides an absolute path to a file (including the file name). I want to get just the file's name. What is the easiest way to do this?It needs to be as general as possible as I cannot know in advance what the URL will be. I can't simply create a URL object and use
getFile()
- all though that would have been ideal if it was possible - as it's not necessarily anhttp://
prefix it could be c:/ or something similar. -
Ravi Wallau almost 15 yearsAlways use it. Can't complain.
-
Ivan over 11 yearsThe first replaceAll parameter is wrong because the slash is an escape character both in Java String and in regular expressions. It should be fileName.replaceAll("\\\\", "/")...
-
bcmoney over 11 yearsAlso, how was this accepted so many times despite the fact that subString should not be camel cased and should be "substring" all lowercase to reach the native String API's substring method, unless you are using something else here but that should probably be noted.
-
craigrs84 almost 11 yearsI found that the first option (new File(filename).getName()) doesn't work the same on all implementations. I found differences between java running on Windows vs java running on Linux. Specifically the Linux implementation was including \ as part of the filename, and wasn't considering it to be a separator. The second option is safer if you want a cross platform implementation.
-
snooze92 almost 11 yearsHow can this answer be accepted so many times, when no one is yet agreeing about how good(/bad?) it is... While a great one liner using Commons IO is available in two other answers? @kd304, as you were suggesting "project due dates teaches you to type fast", but the least you type, the faster you are! FilenameUtils.getName(path) is the simplest/fastest/best way to go...