Sietse Sietse - 5 months ago 42
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.


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);