Check Directory Exists in Java

In Java, you can use the File class to check if a folder exists. Here is an example code:

import java.io.File;

public class CheckFolderExistence {

    public static void main(String[] args) {
        File folder = new File("path/to/folder");

        if (folder.exists() && folder.isDirectory()) {
            System.out.println("Folder exists");
        } else {
            System.out.println("Folder does not exist");
        }
    }
}

In the code above, a File object is first created to represent the folder to be checked. Then the exists() method is used to check if the folder exists, and the isDirectory() method is used to make sure the object represents a folder and not a file. Finally, the corresponding information is output based on the check result.

bannerAds