Java Resource Path Get getFileFromResource(Class clazz, String path)

Here you can find the source of getFileFromResource(Class clazz, String path)

Description

Method "getFileFromResource" returns a File from the given path relative to the class.

License

Open Source License

Parameter

Parameter Description
clazz a class in the same plugin as the file to find
path the path of the file.

Exception

Parameter Description
IOException an exception
URISyntaxException an exception

Return

the file when found. Can return null. Otherwise will throw an exception

Declaration

public static File getFileFromResource(Class<?> clazz, String path) throws IOException, URISyntaxException 

Method Source Code


//package com.java2s;
// %InstallDIR%\features\org.talend.rcp.branding.%PRODUCTNAME%\%PRODUCTNAME%license.txt

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import org.eclipse.core.runtime.FileLocator;

public class Main {
    /**//from  www  .j  av  a2  s  .  co m
     * Method "getFileFromResource" returns a File from the given path relative to the class. When the path starts with
     * "/", the file can be located in a plugin (in the classpath of the plugin). This method must be used in plugin
     * mode. It will return null when it's called as a java application.
     * 
     * @param clazz a class in the same plugin as the file to find
     * @param path the path of the file.
     * @return the file when found. Can return null. Otherwise will throw an exception
     * @throws IOException
     * @throws URISyntaxException
     */
    public static File getFileFromResource(Class<?> clazz, String path) throws IOException, URISyntaxException {
        URL url = clazz.getResource(path);
        if (url == null) {
            return null;
        }
        URL fileURL = FileLocator.toFileURL(url);
        URI escapedUri = new URI(fileURL.getProtocol(), fileURL.getPath(), fileURL.getQuery());
        return new File(escapedUri);
    }
}

Related

  1. getChildResources(String path)
  2. getClassResource(Class clazz, String resPath)
  3. getClassResources(Class clazz, String resPath)
  4. getExistingResourceAsFile(final String resourcePath)
  5. getFileFromBundle(String bundleName, String resourcePath)
  6. getFileResourcePaths(final Class clazz, final String fileNameRegex)
  7. getPath(final Object resource)
  8. getPathOfResource(Class clazz, String fileName)
  9. getPluginResource(Bundle bundle, String resourcePath)