Java Resource File getFile(Class clazz, String resource)

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

Description

Get a file handle for the class's resource.

License

Open Source License

Declaration

public static final File getFile(Class clazz, String resource) 

Method Source Code


//package com.java2s;
/*//from w w w.j  a v a 2  s  . c  o  m
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.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;

public class Main {
    /**
     * Get a file handle for the class's resource.
     */
    public static final File getFile(Class clazz, String resource) {
        File result = null;

        if (clazz != null) {
            try {
                final URL url = clazz.getResource(resource);
                result = new File(url.toURI());
            } catch (URISyntaxException eat) {
            }
        }

        if (result == null) {
            final String filename = getFilename(clazz, resource);
            result = filename == null ? null : getFile(filename);
        }

        return result;
    }

    /**
     * Get the file referenced by the resource name, dereferencing appropriately
     * if the filename is a valid URL.
     */
    public static File getFile(String resourceName) {
        File result = null;

        try {
            final URL url = new URL(resourceName);
            result = new File(url.toURI());
        } catch (MalformedURLException me) {
            result = new File(resourceName);
        } catch (URISyntaxException ue) {
            result = new File(resourceName);
        } catch (IllegalArgumentException ue) {
            result = new File(resourceName);
        }

        return result;
    }

    /**
     * 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. getBytes(Class contextClass, String resourceName)
  2. getClassResource(Class clazz)
  3. getContentAsString(String resource)
  4. getDefaultWebXmlResourceLocation()
  5. getEntriesFromResourceBundle(String rbName, Map map)
  6. getFile(Class resourceClass, String fileName)
  7. getFile(Class cls, String resource)
  8. getFile(String resource)
  9. getFile(String resourceOrFile, Class cls, boolean deleteTmpOnExit)