Java File isDirectory() Method
Example
Check if a file and a folder are directories:
import java.io.File;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
File myFile = new File("filename.txt");
myFile.createNewFile();
File myFolder = new File("myFolder");
myFolder.mkdir();
System.out.println(myFile.isDirectory());
System.out.println(myFolder.isDirectory());
}
}
Definition and Usage
The isDirectory() method checks if the path points to a directory (folder).
It returns false for a normal file, and for a path that does not exist.
Tip: Use the isFile() method to check if the path is a normal file.
Syntax
public boolean isDirectory()
Parameter Values
None.
Technical Details
| Returns: | true if the path is a directory that exists, otherwise false. |
|---|
Related Pages
❮ File Methods