简体   繁体   中英

Finding all the files of a certain extension in a project's source folder

Hello I am trying to retrieve the names of all the files of the type .mp3 from a source folder(below) 在此处输入图片说明

Below is the code I am using to differentiate the file type and add to the list. However I do not know which parameter I should enter into the directory it should be music folder however I do not know how to represent this.

    File dir = new File("");
    String[] files = dir.list(new FilenameFilter() {
        public boolean accept(File dir, String name) {
            return name.toLowerCase().endsWith(".mp3");
        };
    });
    for(int a = 0; a < files.length; a++)
    {
        musicList.add(files[a]);
    }

Try this.

List<String> result = Files.find(Paths.get("music"), 100,
    (p, a) -> p.toString().toLowerCase().endsWith(".mp3"))
    .map(path -> path.toString())
    .collect(Collectors.toList());

if you are using Servlet :

ServletActionContext.getServletContext().getRealPath("music"); 

if using Spring :

ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource("music").getFile());
System.out.println(file.getAbsolutePath());

For javafx see this:

How to target a file (a path to it) in Java/JavaFX

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM