Java Resource Path Get getResourceFile(String sResourcePath, Class cRefClass)

Here you can find the source of getResourceFile(String sResourcePath, Class cRefClass)

Description

Returns the given resource as a file.

License

Apache License

Parameter

Parameter Description
sResourcePath Path relative to the given class.
cRefClass Reference class.

Return

File object pointing to the given file or null if the file was not found.

Declaration

public static File getResourceFile(String sResourcePath, Class<?> cRefClass) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.io.File;

import java.net.URL;

public class Main {
    /**/*from  w  ww.j av  a  2s. c o  m*/
     * Returns the given resource as a file. This method does not work for classes packaged in a jar
     * file.
     *
     * @param   sResourcePath  Path relative to the given class.
     * @param   cRefClass      Reference class.
     *
     * @return  File object pointing to the given file or <code>null</code> if the file was not
     *          found.
     */
    public static File getResourceFile(String sResourcePath, Class<?> cRefClass) {
        URL url = cRefClass.getResource(sResourcePath);

        if (url == null) {
            return null;
        }

        String filePath = url.getFile();

        if (filePath == null) {
            return null;
        }

        // Replace %20 in the URL with spaces.
        filePath = filePath.replaceAll("%20", " ");

        return new File(filePath);
    }
}

Related

  1. getResourceAsStream(final String path)
  2. getResourceAsStream(String resourcePath, ClassLoader classLoader, Class clazz)
  3. getResourceAsString(String path)
  4. getResourceFile(Class clazz, String relPath)
  5. getResourceFile(final Class baseClass, final String path)
  6. getResourceFilePath(String name)
  7. getResourceFileRelativeToBase(final File baseDir, final String resourcePath)
  8. getResourceListing(Class clazz, String path)
  9. getResourceListing(Class clazz, String path)