簡體   English   中英

在項目的源文件夾中查找某個擴展名的所有文件

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

您好,我正在嘗試從源文件夾中檢索.mp3類型的所有文件的名稱(如下) 在此處輸入圖片說明

下面是我用來區分文件類型並添加到列表中的代碼。 但是我不知道我應該將哪個參數輸入到它應該是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]);
    }

試試這個。

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

如果您使用的是 Servlet :

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

如果使用 Spring :

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

對於 javafx,請參見:

如何在 Java/JavaFX 中定位文件(它的路徑)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM