how to check if a file is a directory in java code example
Example 1: java detect folder or file
File file = new File(path);
boolean exists = file.exists();
boolean isDirectory = file.isDirectory();
boolean isFile = file.isFile();
Example 2: check folder for files java
public void listFilesForFolder(final File folder) {
for (final File fileEntry : folder.listFiles()) {
if (fileEntry.isDirectory()) {
listFilesForFolder(fileEntry);
} else {
System.out.println(fileEntry.getName());
}
}
}
final File folder = new File("/home/you/Desktop");
listFilesForFolder(folder);