1

Hello I am trying to retrieve the names of all the files of the type .mp3 from a source folder(below) enter image description here

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]);
    }

2 Answers2

2

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());
0

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

Community
  • 1
  • 1
Jay Prakash
  • 787
  • 6
  • 22