Reputation: 5966
My problem is ::
From a string like "/usr/folder1/folder2/filename.ext"
I looked at URI and URL interfaces in java.net but could not find anything useful there.
Also, in some cases, my file path can have COMMA, SPACE etc (Windows folders). So, keep that in mind when giving any ideas.
Upvotes: 8
Views: 12047
Reputation: 7859
CommonsIO provides solutions for this problem: FilenameUtils.getName()
, returns the file name + extension.
String filename = FilenameUtils.getName("/usr/folder1/folder2/filename.ext");
System.out.println(filename); // Returns "filename.ext"
Upvotes: 4
Reputation: 316
You could try something like this:
File file = new File("/usr/folder1/folder2/filename.ext");
System.out.println(file.getName());
I wasn't sure whether this would work if the file does not exist, but have just tried it and it appears to work OK.
Upvotes: 15
Reputation: 24759
You should have a look to the File class. Especially to the getName() method.
Upvotes: 3