List of utility methods to do Resource Get
File | getResourceFile(Class clazz, String fileName) get Resource File ClassLoader classLoader = clazz.getClassLoader(); URL resource = classLoader.getResource(fileName); if (resource == null) throw new FileNotFoundException(fileName + " not found"); return new File(resource.toURI()); |
File | getResourceFile(String name) Returns a file handle for the resource with the given name. URI uri = Thread.currentThread().getContextClassLoader().getResource(name).toURI(); return new File(uri); |
URL | getResourceFromBundle(Class> clazz, String location) get Resource From Bundle Objects.requireNonNull(clazz);
Objects.requireNonNull(location);
return FrameworkUtil.getBundle(clazz).getResource(location);
|
URL | getResourceFromFile(String filename) get Resource From File URL url = null; File file = new File(filename); if (file.exists()) { try { url = file.toURL(); } catch (MalformedURLException e) { return null; return url; |
URL | getResourceInBundleOrFragments(Bundle bundle, String name) get Resource In Bundle Or Fragments String dirName = "/"; String fileName = name; int index = name.lastIndexOf('/'); if (index > 0) { dirName = name.substring(0, index); fileName = name.substring(index + 1); } else if (index == 0) { fileName = name.substring(1); ... |
List | getResourceListing(ClassLoader cl) get Resource Listing URL dirURL = cl.getResource(""); if (dirURL != null && dirURL.getProtocol().equals("file")) { File f = new File(dirURL.toURI()); return recurse(f, f.getAbsolutePath().replace('\\', '/')); if (dirURL.getProtocol().equals("jar")) { String jarPath = dirURL.getPath().substring(5, dirURL.getPath().indexOf("!")); JarFile jar = new JarFile(URLDecoder.decode(jarPath, "UTF-8")); ... |
String | getResourceMessage(Locale locale, String resource, String key, Object... params) get Resource Message ResourceBundle bundle = ResourceBundle.getBundle(resource, locale); String msg = bundle.getString(key); if (params != null && params.length > 0) msg = MessageFormat.format(msg, params); return msg; |
Reader | getResourceReader(Class clazz, String name, String charset) get Resource Reader return new InputStreamReader(getResourceStream(clazz, name), charset); |
File | getResources(String archiveName) get Resources String name = archiveName; URL url = Thread.currentThread().getContextClassLoader().getResource(name); if (url == null) { System.err.println("No resources for " + archiveName); return null; try { File file = new File(url.toURI()); ... |
List | getResources(String pkgName) get Resources List<URL> urls = new ArrayList<URL>(); try { ClassLoader cld = Thread.currentThread().getContextClassLoader(); if (cld == null) { throw new ClassNotFoundException("Can't get class loader."); String path = pkgName.replace('.', '/'); Enumeration<URL> resources = cld.getResources(path); ... |