java check if path is directory code example
Example 1: java check if directory exists
Path path = ...;
if (Files.exists(path)) {
// ...
}
Example 2: How to check if a directory exists in java
// How to check if a directory exists in java
import java.io.File;
public class CheckIfExists
{
public static void main(String[] args)
{
try
{
File fl = new File("B:/JavaExamples/CheckIfExists.txt");
fl.createNewFile();
System.out.println(fl.exists());
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
}
Example 3: java detect folder or file
File file = new File(path);
boolean exists = file.exists(); // Check if the file exists
boolean isDirectory = file.isDirectory(); // Check if it's a directory
boolean isFile = file.isFile(); // Check if it's a regular file