Sietse Sietse - 1 year ago 139
Java Question

Get file name from URL

In Java, given a
or a
in the form of
, what is the easiest way to get the file name, minus the extension? So, in this example, I'm looking for something that returns

I can think of several ways to do this, but I'm looking for something that's easy to read and short.

Answer Source

Instead of reinventing the wheel, how about using Apache commons-io:


public class FilenameUtilTest {

    public static void main(String[] args) {
        String url = "";

        String baseName = FilenameUtils.getBaseName(url);
        String extension = FilenameUtils.getExtension(url);

        System.out.println("Basename : " + baseName);
        System.out.println("extension : " + extension);

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download