Here you can find the source of getFileName(final Object resource)
This method gives the resource file name.
Parameter | Description |
---|---|
resource | instance of File, URL, URI, or String path. |
Parameter | Description |
---|---|
MalformedURLException | an exception |
private static String getFileName(final Object resource) throws MalformedURLException
//package com.java2s; /*// www.j a v a 2s . c o m * Geotoolkit - An Open Source Java GIS Toolkit * http://www.geotoolkit.org * * (C) 2008 - 2009, Geomatys * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. */ import java.io.File; import java.net.MalformedURLException; import java.net.URI; import java.net.URL; public class Main { /** * <p>This method gives the resource file name. * Resource can be an instance of File, URL, URI or String path. * Return snull in other cases.</p> * * @param resource instance of File, URL, URI, or String path. * @return * @throws MalformedURLException */ private static String getFileName(final Object resource) throws MalformedURLException { String fileName = null; if (resource instanceof File) { fileName = ((File) resource).getName(); } else { fileName = getPath(resource); fileName = fileName.substring(fileName.lastIndexOf(File.separator) + 1, fileName.length()); } return fileName; } /** * <p>This method returns the path of a given resource which can be * instance of File, URL, URI or String. Returns null in other cases.</p> * * @param resource instance of File, URL, URI, or String path. * @return The resource path. * @throws MalformedURLException */ private static String getPath(final Object resource) throws MalformedURLException { String extractPath = null; if (resource instanceof File) { extractPath = ((File) resource).getPath(); } else if (resource instanceof URL) { extractPath = ((URL) resource).getPath(); } else if (resource instanceof URI) { extractPath = (((URI) resource).toURL()).getPath(); } else if (resource instanceof String) { extractPath = (String) resource; } return extractPath; } }