carles xuriguera
carles xuriguera

Reputation: 980

How to read all files from the resource folder of a SpringBoot app?

I have a Spring Boot v2.1.2.RELEASE application. I have this folder ../src/main/resources/icons/svg/white/

I am trying to list all the files of the folder But It seems that the folder does not exist

@SpringBootApplication
public class SvgManagerApplication implements CommandLineRunner {
    public static void main(String[] args) {
        SpringApplication.run(SvgManagerApplication.class, args);
    }

    @Override
    public void run(String... args) throws Exception {      
        try {
            File folder = new File("icons/svg/white/");
            listFilesForFolder(folder);
        } catch (IOException ex) {
            System.out.println(ex.getMessage());
        }
    }

    public void listFilesForFolder(final File folder) {
        for (final File fileEntry : folder.listFiles()) {
            if (fileEntry.isDirectory()) {
                listFilesForFolder(fileEntry);
            } else {
                System.out.println(fileEntry.getName());
            }
        }
    }    
}

Upvotes: 1

Views: 3138

Answers (1)

Bourbia Brahim
Bourbia Brahim

Reputation: 14712

use the ResourceUtils

File folder= ResourceUtils.getFile("classpath:icons/svg/white/");

Upvotes: 4

Related Questions