Close

Java - Find Files in classpath under a Folder And SubFolder

[Last Updated: Sep 24, 2020]

Java Spring Framework 

This tutorial shows how to get the list of resources (files) from a classpath directory and subdirectory.

Examples

Example resource directory structure

D:\files-in-classpath-under-folder-and-subfolder>tree /a /f src/main/resources
Folder PATH listing
Volume serial number is B2F7-682A
D:\FILES-IN-CLASSPATH-UNDER-FOLDER-AND-SUBFOLDER\SRC\MAIN\RESOURCES
| a.txt
|
+---subfolder1
| b.txt
|
\---subfolder2
| c.txt
|
\---subsubfolder21
d.txt

Using plain Java

package com.logicbig.example;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;

public class FilesInClassPathJavaExample {

  public static void main(String[] args) {
      System.out.println("-- under subfolder2 --");
      List<File> resourceFolderFiles = getResourceFolderFiles("subfolder2");
      resourceFolderFiles.stream().filter(f -> f.getName().endsWith(".txt")).forEach(System.out::println);
      System.out.println("-- under all folders in classpath --");
      resourceFolderFiles = FilesInClassPathJavaExample.getResourceFolderFiles("");
      resourceFolderFiles.stream().filter(f -> f.getName().endsWith(".txt")).forEach(System.out::println);
  }

  static List<File> getResourceFolderFiles(String folder) {
      ClassLoader loader = Thread.currentThread().getContextClassLoader();
      URL url = loader.getResource(folder);
      String path = url.getPath();
      File file = new File(path);
      List<File> files = new ArrayList<>();
      if(file.isDirectory()){
          try {
              Files.walk(file.toPath()).filter(Files::isRegularFile).forEach(f -> files.add(f.toFile()));
          } catch (IOException e) {
              throw new RuntimeException(e);
          }
      }else{
        files.add(file);
      }
     return files;
  }
}
-- under subfolder2 --
D:\files-in-classpath-under-folder-and-subfolder\target\classes\subfolder2\c.txt
D:\files-in-classpath-under-folder-and-subfolder\target\classes\subfolder2\subsubfolder21\d.txt
-- under all folders in classpath --
D:\files-in-classpath-under-folder-and-subfolder\target\classes\a.txt
D:\files-in-classpath-under-folder-and-subfolder\target\classes\subfolder1\b.txt
D:\files-in-classpath-under-folder-and-subfolder\target\classes\subfolder2\c.txt
D:\files-in-classpath-under-folder-and-subfolder\target\classes\subfolder2\subsubfolder21\d.txt

Using Spring PathMatchingResourcePatternResolver

package com.logicbig.example;

import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import java.io.File;
import java.io.IOException;

public class FilesInClassPathSpringExample {

    public static void main(String[] args) throws IOException {
        System.out.println("-- under subfolder2 --");
        PathMatchingResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
        Resource[] resources = resourcePatternResolver.getResources("classpath:subfolder2/**/*.txt");
        for (Resource resource : resources) {
            File file = resource.getFile();
            System.out.println(file);
        }

        System.out.println("-- under all folders in classpath --");
        PathMatchingResourcePatternResolver resourcePatternResolver2 = new PathMatchingResourcePatternResolver();
        Resource[] resources2 = resourcePatternResolver.getResources("classpath:**/*.txt");
        for (Resource resource : resources2) {
            File file = resource.getFile();
            System.out.println(file);
        }
    }
}
-- under subfolder2 --
D:\files-in-classpath-under-folder-and-subfolder\target\classes\subfolder2\c.txt
D:\files-in-classpath-under-folder-and-subfolder\target\classes\subfolder2\subsubfolder21\d.txt
-- under all folders in classpath --
D:\files-in-classpath-under-folder-and-subfolder\target\classes\a.txt
D:\files-in-classpath-under-folder-and-subfolder\target\classes\subfolder1\b.txt
D:\files-in-classpath-under-folder-and-subfolder\target\classes\subfolder2\c.txt
D:\files-in-classpath-under-folder-and-subfolder\target\classes\subfolder2\subsubfolder21\d.txt

Example Project

Dependencies and Technologies Used:

  • spring-core 5.2.9.RELEASE: Spring Core.
  • JDK 8
  • Maven 3.5.4

Finding files recursively in classpath Select All Download
  • files-in-classpath-under-folder-and-subfolder
    • src
      • main
        • java
          • com
            • logicbig
              • example
                • FilesInClassPathJavaExample.java
          • resources
            • subfolder1
            • subfolder2
              • subsubfolder21

    See Also