List of utility methods to do Resource Path Get
File | getExistingResourceAsFile(final String resourcePath) Create a java.io.File object for the input resourcePath argument. URL url = System.class.getResource(resourcePath); if (url != null) { try { return new File(url.toURI()); } catch (URISyntaxException e) { URL referenceURL = System.class.getResource(knownRootResourcePath); ... |
File | getFileFromBundle(String bundleName, String resourcePath) Get File from bundle if exists Bundle bundle = Platform.getBundle(bundleName); URL fileURL = bundle.getEntry(resourcePath); URL resolvedFileURL = FileLocator.toFileURL(fileURL); URI resolvedURI = new URI(resolvedFileURL.getProtocol(), resolvedFileURL.getPath(), null); File file = new File(resolvedURI); return file; |
File | getFileFromResource(Class> clazz, String path) Method "getFileFromResource" returns a File from the given path relative to the class. 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); |
List | getFileResourcePaths(final Class> clazz, final String fileNameRegex) Collect the full resource paths of files, matching the given regular expression, in the package of the specified class. final List<String> paths = new LinkedList<String>(); final Pattern pattern = Pattern.compile(fileNameRegex); final File jarFile = new File(clazz.getProtectionDomain().getCodeSource().getLocation().getPath()); String packagePath = clazz.getPackage().getName().replaceAll("[.]", "/"); if (jarFile.isFile()) { JarFile jar = null; try { jar = new JarFile(jarFile); ... |
String | getPath(final Object resource) This method returns the path of a given resource which can be instance of File, URL, URI or String. String extractPath = null; if (resource instanceof File) { extractPath = ((File) resource).getPath(); } else if (resource instanceof URL) { extractPath = ((URL) resource).getPath(); } else if (resource instanceof URI) { extractPath = (((URI) resource).toURL()).getPath(); } else if (resource instanceof String) { ... |
String | getPathOfResource(Class> clazz, String fileName) get Path Of Resource URL url = getUrlAsResource(clazz, fileName); if (url == null) return null; return url.getPath(); |
URL | getPluginResource(Bundle bundle, String resourcePath) get Plugin Resource return bundle.getResource(resourcePath);
|
File | getRelativeResource(Class> clazz, String relativePath) get Relative Resource String packageName = clazz.getPackage().getName(); String packagePath = packageName.replaceAll("\\.", "/"); String wholePath = packagePath + "/" + relativePath; URL url = Thread.currentThread().getContextClassLoader().getResource(wholePath); if (url == null) { String message = String.format("File [%s] not found", wholePath); throw new IllegalArgumentException(message); return new File(url.getPath()); |
URL | getResource(Class> cl, String path) get Resource String clp = cl.getName().replace('.', '/') + ".class"; URL clu = cl.getClassLoader().getResource(clp); String s = clu.toString(); if (s.endsWith(clp)) return new URL(s.substring(0, s.length() - clp.length()) + path); if (s.startsWith("jar:")) { String[] ss = s.split("!"); return new URL(ss[1] + "!/" + path); ... |
URL | getResource(final Class> baseClass, final String path) get Resource final URL url = baseClass.getClassLoader().getResource(path); return url; |