Check if a path represents a file or a folder Check if a path represents a file or a folder android android

Check if a path represents a file or a folder


Assuming path is your String.

File file = new File(path);boolean exists =      file.exists();      // Check if the file existsboolean isDirectory = file.isDirectory(); // Check if it's a directoryboolean isFile =      file.isFile();      // Check if it's a regular file

See File Javadoc


Or you can use the NIO class Files and check things like this:

Path file = new File(path).toPath();boolean exists =      Files.exists(file);        // Check if the file existsboolean isDirectory = Files.isDirectory(file);   // Check if it's a directoryboolean isFile =      Files.isRegularFile(file); // Check if it's a regular file


Clean solution while staying with the nio API:

Files.isDirectory(path)Files.isRegularFile(path)


Please stick to the nio API to perform these checks

import java.nio.file.*;static Boolean isDir(Path path) {  if (path == null || !Files.exists(path)) return false;  else return Files.isDirectory(path);}