
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
How to get the names of the empty directories in a directory in Java?
The ListFiles() method returns an array holding the objects (abstract paths) of all the files (and directories) in the path represented by the current (File) object.
The File Filter interface is filter for the path names you can pass this as a parameter to the listFiles() method. This method filters the file names passed on the passed filter.
To get the directories in a folder implement a FileFilter which accepts only empty directories, and pass it as a parameter to the listFiles() method.
Example
import java.io.File; import java.io.FileFilter; import java.io.IOException; public class MyExample{ public static void main(String args[]) throws IOException { //Creating a File object for directory File directoryPath = new File("D:\ExampleDirectory"); //Creating filter for directories files FileFilter fileFilter = new FileFilter(){ public boolean accept(File dir) { if (dir.isDirectory()&& dir.list().length==0) { return true; } else { return false; } } }; File[] list = directoryPath.listFiles(fileFilter); System.out.println("List of the jpeg files in the specified directory:"); for(File fileName : list) { System.out.println(fileName.getName()); System.out.println(fileName); } } }
Output
List of the jpeg files in the specified directory: sample directory1 D:\ExampleDirectory\sample directory1
- Related Articles
- How to get a list of all sub-directories in the current directory using Python?
- How to get Directories from JFileChooser in Java
- Python Program to Display all the directories in a directory
- Golang Program to Display all the directories in a directory
- How to get the list of jpg files in a directory in Java?
- How to get the directories (only) from a folder using Java?
- How to get all the directories and sub directories inside a path in C#?
- Get Base Root Directories of a System in Java
- Java Program to get the content of a directory
- Check if a directory is not empty in Java
- Get the Current Working Directory in Java
- Java Program to get the name of the parent directory of the file or directory
- How to Add an Empty Directory in Git
- How to get the home directory in Python?
- Delete empty files and directories in Linux

Advertisements