List of utility methods to do ClassLoader Load
List | getDirectories(String path) Locate all directories in given package ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); assert classLoader != null; Enumeration<URL> resources = classLoader.getResources(path); List<URL> dirs = new ArrayList<URL>(); while (resources.hasMoreElements()) { dirs.add(resources.nextElement()); return dirs; ... |
File | getFile(final String filename) Get a File for referencing a file on the classpath. final URL url = getURL(filename); return new File(url.toURI()); |
File | getFile(final String name) get File final File testFile = new File(Thread.currentThread().getContextClassLoader() .getResource("resources" + File.separator + name).toURI()); return testFile; |
File | getFile(Object obj, String filename) get File String file = obj.getClass().getPackage().getName().replace('.', '/') + '/' + filename; URL url = obj.getClass().getClassLoader().getResource(file); return new File(url.getFile()); |
String | getFileContents(String filename) get File Contents StringBuffer sb = new StringBuffer(); String p = "xsp/extlib/designer/junit/resources/" + filename; ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader(); int ch; InputStream in = null; try { URL url = contextClassLoader.getResource(p); in = url.openStream(); ... |
String | getFileContents(String path) Uses the toURL() call to convert the path and as a result can find files on the classpath. URL url = toURL(path); if (url == null) throw new IOException("File not found: " + path); return getFileContents(new FileInputStream(url.getPath())); |
File | getFileFromPath(Object obj, String fileName) get File From Path ClassLoader classLoader = obj.getClass().getClassLoader(); URL resource = classLoader.getResource(fileName); return new File(resource.getPath()); |
InputStream | getFileFromPool(String path) get File From Pool return loader.getResourceAsStream(path);
|
String | getFileFullPath(String file) get File Full Path String path = ResourcePath(file); File fileTmp = new File(path); if (fileTmp.exists()) { return path; return null; |
List | getFilesInPackage(String packageName) Given a package name, attempts to reflect to find all file names within the package on the local file system. List<String> fns = new ArrayList<>(); String packageNameSlashed = packageName.replace(".", "/"); URL directoryURL = Thread.currentThread().getContextClassLoader().getResource(packageNameSlashed); if (directoryURL == null) { System.out.println("Could not retrieve URL resource: " + packageNameSlashed); return fns; String directoryString = directoryURL.getFile(); ... |