Get a list of resources from classpath directory

前端 未结 14 861
予麋鹿
予麋鹿 2020-11-22 05:20

I am looking for a way to get a list of all resource names from a given classpath directory, something like a method List getResourceNames (String direct

相关标签:
14条回答
  • 2020-11-22 06:00

    Neither of answers worked for me even though I had my resources put in resources folders and followed the above answers. What did make a trick was:

    @Value("file:*/**/resources/**/schema/*.json")
    private Resource[] resources;
    
    0 讨论(0)
  • 2020-11-22 06:01

    This should work (if spring is not an option):

    public static List<String> getFilenamesForDirnameFromCP(String directoryName) throws URISyntaxException, UnsupportedEncodingException, IOException {
        List<String> filenames = new ArrayList<>();
    
        URL url = Thread.currentThread().getContextClassLoader().getResource(directoryName);
        if (url != null) {
            if (url.getProtocol().equals("file")) {
                File file = Paths.get(url.toURI()).toFile();
                if (file != null) {
                    File[] files = file.listFiles();
                    if (files != null) {
                        for (File filename : files) {
                            filenames.add(filename.toString());
                        }
                    }
                }
            } else if (url.getProtocol().equals("jar")) {
                String dirname = directoryName + "/";
                String path = url.getPath();
                String jarPath = path.substring(5, path.indexOf("!"));
                try (JarFile jar = new JarFile(URLDecoder.decode(jarPath, StandardCharsets.UTF_8.name()))) {
                    Enumeration<JarEntry> entries = jar.entries();
                    while (entries.hasMoreElements()) {
                        JarEntry entry = entries.nextElement();
                        String name = entry.getName();
                        if (name.startsWith(dirname) && !dirname.equals(name)) {
                            URL resource = Thread.currentThread().getContextClassLoader().getResource(name);
                            filenames.add(resource.toString());
                        }
                    }
                }
            }
        }
        return filenames;
    }
    
    0 讨论(0)
  • 2020-11-22 06:04

    Used a combination of Rob's response.

    final String resourceDir = "resourceDirectory/";
    List<String> files = IOUtils.readLines(Thread.currentThread().getClass().getClassLoader().getResourceAsStream(resourceDir), Charsets.UTF_8);
    
    for(String f : files){
      String data= IOUtils.toString(Thread.currentThread().getClass().getClassLoader().getResourceAsStream(resourceDir + f));
      ....process data
    }
    
    0 讨论(0)
  • 2020-11-22 06:05

    With Spring it's easy. Be it a file, or folder, or even multiple files, there are chances, you can do it via injection.

    This example demonstrates the injection of multiple files located in x/y/z folder.

    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.core.io.Resource;
    import org.springframework.stereotype.Service;
    
    @Service
    public class StackoverflowService {
        @Value("classpath:x/y/z/*")
        private Resource[] resources;
    
        public List<String> getResourceNames() {
            return Arrays.stream(resources)
                    .map(Resource::getFilename)
                    .collect(Collectors.toList());
        }
    }
    

    It does work for resources in the filesystem as well as in JARs.

    0 讨论(0)
  • 2020-11-22 06:08

    The most robust mechanism for listing all resources in the classpath is currently to use this pattern with ClassGraph, because it handles the widest possible array of classpath specification mechanisms, including the new JPMS module system. (I am the author of ClassGraph.)

    List<String> resourceNames;
    try (ScanResult scanResult = new ClassGraph().whitelistPaths("x/y/z").scan()) {
        resourceNames = scanResult.getAllResources().getNames();
    }
    
    0 讨论(0)
  • 2020-11-22 06:11

    Based on @rob 's information above, I created the implementation which I am releasing to the public domain:

    private static List<String> getClasspathEntriesByPath(String path) throws IOException {
        InputStream is = Main.class.getClassLoader().getResourceAsStream(path);
    
        StringBuilder sb = new StringBuilder();
        while (is.available()>0) {
            byte[] buffer = new byte[1024];
            sb.append(new String(buffer, Charset.defaultCharset()));
        }
    
        return Arrays
                .asList(sb.toString().split("\n"))          // Convert StringBuilder to individual lines
                .stream()                                   // Stream the list
                .filter(line -> line.trim().length()>0)     // Filter out empty lines
                .collect(Collectors.toList());              // Collect remaining lines into a List again
    }
    

    While I would not have expected getResourcesAsStream to work like that on a directory, it really does and it works well.

    0 讨论(0)
提交回复
热议问题