0
public class Sorter {
String dir1 = ("C:/Users/Drew/Desktop/test");
String dir2 = ("C:/Users/Drew/Desktop/");

public void SortingAlgo() throws IOException {

// Declare files for moving
File sourceDir = new File(dir1);
File destDir = new File(dir2);

//Get files, list them, grab only mp3 out of the pack, and sort

File[] listOfFiles = sourceDir.listFiles();

if(sourceDir.isDirectory()) {

for(int i = 0; i < listOfFiles.length; i++) {

    //list Files
    System.out.println(listOfFiles[i]);

    String ext = FilenameUtils.getExtension(dir1);
    System.out.println(ext);



    }
}

}

}

I am trying to filter out only .mp3's in my program. I'm obviously a beginner and tried copying some things off of Google and this website. How can I set a directory (sourceDir) and move those filtered files to it's own folder?

user1985295
  • 13
  • 1
  • 4
  • Have you looked at suggested solution here: [http://stackoverflow.com/questions/1146153/copying-files-from-one-directory-to-another-in-java] or here: [http://stackoverflow.com/questions/5758268/java-move-file-with-certain-file-extension] ? – PM 77-1 Aug 21 '13 at 23:26
  • 1) Use a consistent and logical indent for code blocks. The indentation of the code is intended to help people understand the program flow. 2) For better help sooner, post an [SSCCE](http://sscce.org/) (that does not use 3rd party APIs like `FileUtils`). 3) Look to [`File.listFiles(FilenameFilter)`](http://docs.oracle.com/javase/7/docs/api/java/io/File.html#listFiles%28java.io.FilenameFilter%29) for filtering the `.mp3` files from the rest. 4) The title of the post does not match up well with the question. Could you change one or the other? – Andrew Thompson Aug 21 '13 at 23:26

3 Answers3

2

I find the NIO.2 approach using GLOBs or custom filter the cleanest solution. Check out this example on how to use GLOB or filter example in the attached link:

Path directoryPath = Paths.get("C:", "Program Files/Java/jdk1.7.0_40/src/java/nio/file");

if (Files.isDirectory(directoryPath)) {
    try (DirectoryStream<Path> stream = Files.newDirectoryStream(directoryPath, "*.mp3")) {
        for (Path path : stream) {
            System.out.println(path);
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

For more information about content listing and directory filtering visit Listing and filtering directory contents in NIO.2

Jasty
  • 81
  • 1
  • 3
1

File provides an ability to filter the file list as it's begin generated.

File[] listOfFiles = sourceDir.listFiles(new FileFilter() {
    @Override
    public boolean accept(File pathname) {
        return pathname.getName().toLowerCase().endsWith(".mp3");
    }
});

Now, this has a number of benefits, the chief among which is you don't need to post-process the list, again, or have two lists in memory at the same time.

It also provides pluggable capabilities. You could create a MP3FileFilter class for instance and re-use it.

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Generally speaking, does `listFiles` return all of the files within a directory as well? – Josh M Aug 21 '13 at 23:47
  • @JoshM Generally speaking `listFiles` has to iterate all the files in the directory any way to generate the list. The `FileFilter` allows the method to discard those `File`s you don't want and return only those you do. This means you don't need to double process the list... – MadProgrammer Aug 22 '13 at 00:31
0
if(ext.endWith(".mp3")){
 //do what ever you want 
}
Caffe Latte
  • 1,693
  • 1
  • 14
  • 32
  • This is no good. Imagine you need to process only .mp3 files and you have 10^10 archives and only one of these ends with .mp3. With your method you will do 10^10 innecesary if statements. – ESCM Jun 03 '21 at 05:39