Path filePath = Paths.get("file.txt"); if (Files.notExists(filePath)) { System.out.println("File does not exist"); }
Path dirPath = Paths.get("directory"); if (Files.notExists(dirPath)) { System.out.println("Directory does not exist"); }This example is similar to the previous one, except we are checking for the existence of a directory instead of a file. This method belongs to the "java.nio.file" package library.