简体   繁体   English

在项目的源文件夹中查找某个扩展名的所有文件

[英]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)您好,我正在尝试从源文件夹中检索.mp3类型的所有文件的名称(如下) 在此处输入图片说明

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.但是我不知道我应该将哪个参数输入到它应该是music文件夹的directory ,但是我不知道如何表示这一点。

    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 :如果您使用的是 Servlet :

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

if using Spring :如果使用 Spring :

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

For javafx see this:对于 javafx,请参见:

How to target a file (a path to it) in Java/JavaFX 如何在 Java/JavaFX 中定位文件(它的路径)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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