Java Resource File getFilename(Class clazz, String resource)

Here you can find the source of getFilename(Class clazz, String resource)

Description

Get the filename for the class's resource suitable for opening.

License

Open Source License

Declaration

public static String getFilename(Class clazz, String resource) 

Method Source Code


//package com.java2s;
/*//from  w w w.j a  v a2 s .c  om
Copyright 2009 Semantic Discovery, Inc. (www.semanticdiscovery.com)
    
This file is part of the Semantic Discovery Toolkit.
    
The Semantic Discovery Toolkit 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 3 of the License, or
(at your option) any later version.
    
The Semantic Discovery Toolkit 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.
    
You should have received a copy of the GNU Lesser General Public License
along with The Semantic Discovery Toolkit.  If not, see <http://www.gnu.org/licenses/>.
*/

import java.io.*;

import java.net.URL;

public class Main {
    /**
     * Get the filename for the class's resource suitable for opening.
     */
    public static String getFilename(Class clazz, String resource) {
        String filename = null;
        if (clazz == null) {
            filename = resource;
        } else {
            final URL url = getUrl(clazz, resource);
            if (url != null) {
                filename = url.toString();
            }
        }

        return filename;
    }

    /**
     * Extract the file name from the full path.
     */
    public static String getFileName(String fullPath) {
        String result = null;
        if (fullPath != null) {
            final int pos = fullPath.lastIndexOf(File.separatorChar);
            if (pos >= 0) {
                result = fullPath.substring(pos + 1);
            }
        }
        return result;
    }

    /**
     * Concatenate the base file name to the given path.
     * <p>
     * Assume fullPath is the path to a directory and deal appropriately with
     * the presence or absence of a trailing slash on the path.
     */
    public static String getFilename(String fullPath, String baseName) {
        final boolean hasTrailingSlash = (fullPath.charAt(fullPath.length() - 1) == File.separatorChar);
        return hasTrailingSlash ? (fullPath + baseName) : (fullPath + File.separatorChar + baseName);
    }

    /**
     * Concatenate the file name to the parent for a full absolute path.
     */
    public static String getFilename(File parent, String name) {
        return getFilename(parent.getAbsolutePath(), name);
    }

    public static URL getUrl(Class clazz, String resource) {
        if (File.separatorChar != '/') {
            if (File.separatorChar == '\\') {
                //resource = resource.replaceAll("/", "\\\\");
            } else {
                //resource = resource.replaceAll("/", File.separator);
            }
        }
        return clazz.getResource(resource);
    }
}

Related

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