Skip to main content Link Menu Expand (external link) Document Search Copy Copied

List files and directories

Header Image

Ahoy there, mateys! As we sail through our journey of learning the ins and outs of Apache Commons IO, it’s time to delve into the topic of listing files and directories using the library.

Listing files and directories using the library

Have you ever found yourself lost at sea, surrounded by a sea of files and directories, unsure of which one to navigate towards? Fear not, as Apache Commons IO provides us with the tools to list all files and directories within a specified directory.

Using the FileUtils class, we can easily list all files within a directory by calling the listFiles method and passing in the path to the directory as a File object. Let’s take a look at an example:

import org.apache.commons.io.FileUtils;

import java.io.File;

public class FileListingExample {
    public static void main(String[] args) {
        File directory = new File("/path/to/directory");
        File[] files = FileUtils.listFiles(directory, null, false);
        for (File file : files) {
            System.out.println(file.getName());
        }
    }
}

In the above example, we first create a File object representing the directory we want to list files from. We then call the listFiles method from the FileUtils class, passing in the directory File object as the first parameter, null as the second parameter (indicating that we want all files), and false as the third parameter (indicating that we don’t want to list files recursively).

The listFiles method returns an array of File objects, representing all the files in the directory. We then loop through the array and print out the name of each file using the getName method.

Similarly, we can list all directories within a specified directory by calling the listFiles method with the FileFilter parameter set to DirectoryFileFilter.INSTANCE. Let’s take a look at an example:

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.filefilter.DirectoryFileFilter;

import java.io.File;

public class DirectoryListingExample {
    public static void main(String[] args) {
        File directory = new File("/path/to/directory");
        File[] directories = FileUtils.listFiles(directory, DirectoryFileFilter.INSTANCE, null);
        for (File dir : directories) {
            System.out.println(dir.getName());
        }
    }
}

In the above example, we use the same FileUtils method as before, but this time we pass in DirectoryFileFilter.INSTANCE as the second parameter, indicating that we only want to list directories. We also pass in null as the third parameter, indicating that we want all directories.

The listFiles method again returns an array of File objects, representing all the directories in the directory. We then loop through the array and print out the name of each directory using the getName method.

Conclusion

Now that we’ve learned how to list files and directories using Apache Commons IO, we can confidently navigate our way through any file or directory structure. Remember to use the FileUtils class and its listFiles method with the appropriate parameters to list files or directories, and always be prepared for any adventure that comes your way. Happy sailing, mateys!