Tuesday, July 7, 2026
HomeLanguagesJavaHow to Scan All Mp3 Files In the Given Directory using Java?

How to Scan All Mp3 Files In the Given Directory using Java?

In this article, we will discuss how to scan all mp3 files and not only from a specific path for an android media player. We will use a recursive method for scanning files. Scan all folders and sort out all mp3 files using the File and FileFilter class.

Implementation

Java




import java.io.File;
import java.io.FileFilter;
  
// Java program for scan all mp3 files in given directory
  
public class Main {
  
    public static final String C_DIRECTORY = "C:\\";
  
    public static void main(String[] args)
    {
        scanAllFile(C_DIRECTORY);
    }
  
    // recursive function for scan all files in directory
    public static void scanAllFile(String path)
    {
        File file = new File(path);
  
        File[] files = file.listFiles();
        if (files == null)
            return;
  
        for (File f : files) {
  
            if (f.isDirectory() && f.exists()) {
                try {
                    scanAllFile(f.getPath());
                }
                catch (Exception e) {
                    e.printStackTrace();
                    continue;
                }
            }
            else if (!f.isDirectory() && f.exists()) {
                // using file filter
                if (filter.accept(f)) {
                    System.out.println(f.getName());
                }
            }
        }
    }
  
    // file filter for sort mp3 files
    static FileFilter filter = new FileFilter() {
        @Override 
          public boolean accept(File file)
        {
            if (file.getName().endsWith(".mp3")
                || file.getName().endsWith(".mp3")) {
                return true;
            }
            return false;
        }
    };
}


Output:

break.mp3
error.mp3
foldedAreas.mp3
quickFixes.mp3
taskEnded.mp3
terminalBell.mp3
warning.mp3
house_lo.mp3
Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

2 COMMENTS

Most Popular

Dominic
32519 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6900 POSTS0 COMMENTS
Nicole Veronica
12015 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12110 POSTS0 COMMENTS
Shaida Kate Naidoo
7019 POSTS0 COMMENTS
Ted Musemwa
7263 POSTS0 COMMENTS
Thapelo Manthata
6976 POSTS0 COMMENTS
Umr Jansen
6968 POSTS0 COMMENTS