How to list the files inside a JAR file?

前端 未结 16 2231
温柔的废话
温柔的废话 2020-11-22 00:40

I have this code which reads all the files from a directory.

    File textFolder = new File(\"text_directory\");

    File [] texFiles = textFolder.listFiles         


        
16条回答
  •  醉话见心
    2020-11-22 01:43

    Here is an example of using Reflections library to recursively scan classpath by regex name pattern augmented with a couple of Guava perks to to fetch resources contents:

    Reflections reflections = new Reflections("com.example.package", new ResourcesScanner());
    Set paths = reflections.getResources(Pattern.compile(".*\\.template$"));
    
    Map templates = new LinkedHashMap<>();
    for (String path : paths) {
        log.info("Found " + path);
        String templateName = Files.getNameWithoutExtension(path);
        URL resource = getClass().getClassLoader().getResource(path);
        String text = Resources.toString(resource, StandardCharsets.UTF_8);
        templates.put(templateName, text);
    }
    

    This works with both jars and exploded classes.

提交回复
热议问题