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