In Java, given a java.net.URL
or a String
in the form of http://www.example.com/some/path/to/a/file.xml
, what is the easiest way to get the file name, minus the extension? So, in this example, I'm looking for something that returns "file"
.
I can think of several ways to do this, but I'm looking for something that's easy to read and short.
Get File Name with Extension, without Extension, only Extension with just 3 line:
Log Result:
Hope it will help you.
Here is the simplest way to do it in Android. I know it will not work in Java but It may help Android application developer.
I've found that some urls when passed directly to
FilenameUtils.getName
return unwanted results and this needs to be wrapped up to avoid exploits.For example,
returns
which I doubt anyone wants to allow.
The following function seems to work fine, and shows some of these test cases, and it returns
null
when the filename can't be determined.This is wrapped up with some simple tests cases in the following example:
How about this:
import java.io.*;
I hope this will help you.
In order to return filename without extension and without parameters use the following:
In order to return filename with extension without params use this: