Here you can find the source of getFileFromResource(Class> clazz, String path)
Parameter | Description |
---|---|
clazz | a class in the same plugin as the file to find |
path | the path of the file. |
Parameter | Description |
---|---|
IOException | an exception |
URISyntaxException | an exception |
public static File getFileFromResource(Class<?> clazz, String path) throws IOException, URISyntaxException
//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); } }