List of utility methods to do Resource File
void | getEntriesFromResourceBundle(String rbName, Map map) This method loads the resource bundle & puts all the values in map ResourceBundle rb = ResourceBundle.getBundle(rbName);
for (Enumeration e = rb.getKeys(); e.hasMoreElements();) {
String key = (String) e.nextElement();
map.put(key, rb.getString(key));
|
File | getFile(Class clazz, String resource) Get a file handle for the class's resource. File result = null; if (clazz != null) { try { final URL url = clazz.getResource(resource); result = new File(url.toURI()); } catch (URISyntaxException eat) { if (result == null) { final String filename = getFilename(clazz, resource); result = filename == null ? null : getFile(filename); return result; |
File | getFile(Class resourceClass, String fileName) Returns a File named fileName associated with Class resourceClass . try { return new File(URLDecoder.decode(resourceClass.getResource(fileName).getFile(), "UTF-8")); } catch (UnsupportedEncodingException e) { return new File(resourceClass.getResource(fileName).getFile()); |
File | getFile(Class> cls, String resource) get File URL url = cls.getResource(resource); if (url == null) return null; return new File(url.getFile()); |
File | getFile(String resource) Multi protocol resource loader. File directFile = new File(resource); if (directFile.exists()) { return directFile; URL classLoader = Thread.currentThread().getContextClassLoader().getResource(resource); if (classLoader == null) { throw new FileNotFoundException("Unable to locate '" + resource + "' as direct File or on classpath"); String fromURL = classLoader.getFile(); if (fromURL == null || fromURL.isEmpty()) { throw new FileNotFoundException("Unable to convert URL '" + fromURL + "' to File"); return new File(fromURL); |
File | getFile(String resourceOrFile, Class> cls, boolean deleteTmpOnExit) Use getInputStream instead, because Files are trouble, when in (maven) JARs try { URI uri = getURL(resourceOrFile, cls).toURI(); String uriStr = uri.toString(); if (uriStr.startsWith("jar")) { if (uriStr.endsWith("/")) { throw new UnsupportedOperationException("cannot unjar directories, only files"); String jarPath = uriStr.substring(4, uriStr.indexOf("!")).replace("file:", ""); ... |
File | getFileByResources(String fileName, Class clazz) get File By Resources String strFile = clazz.getResource(fileName).getFile(); return new File(URLDecoder.decode(strFile, "utf-8")); |
File | getFileFromRelativeResource(final Class> type, final String relativeResourceName) Returns the specified file from a resource relative to the given type. final URL url = type.getResource(relativeResourceName); final File file = getFileFrom(url); return file; |
File | getFileFromSystemResources(String fileName) get File From System Resources File file = null; ClassLoader classLoader = ClassLoader.getSystemClassLoader(); if (classLoader != null) { URL url = classLoader.getResource(fileName); if (url == null) { url = classLoader.getResource(File.separator + fileName); try { ... |
String | getFilename(Class clazz, String resource) Get the filename for the class's resource suitable for opening. String filename = null; if (clazz == null) { filename = resource; } else { final URL url = getUrl(clazz, resource); if (url != null) { filename = url.toString(); return filename; |