Java Resource File getFileName(final Object resource)

Here you can find the source of getFileName(final Object resource)

Description

This method gives the resource file name.

License

Open Source License

Parameter

Parameter Description
resource instance of File, URL, URI, or String path.

Exception

Parameter Description
MalformedURLException an exception

Declaration

private static String getFileName(final Object resource) throws MalformedURLException 

Method Source Code

//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;
    }
}

Related

  1. getFile(String resourceOrFile, Class cls, boolean deleteTmpOnExit)
  2. getFileByResources(String fileName, Class clazz)
  3. getFileFromRelativeResource(final Class type, final String relativeResourceName)
  4. getFileFromSystemResources(String fileName)
  5. getFilename(Class clazz, String resource)
  6. getFileResource(String resourceName)
  7. getInputStream(String resourceOrFile, Class cls)
  8. getInputStreamForResource(String s)
  9. getLastModificationForResource(String resourceId)