Does file exists utility function in Java

Views: 56   

We often check whether or not a given file exists in the filesystem. This Post gives a utility function using which you can check whether or not a given file with path exists?

public static boolean doesFileExists(String path) {
        if (path != null && !path.isEmpty()) {
            return Files.exists(Paths.get(path));
        }
        return false;
    }

Using the util method in action

import java.nio.file.Files;
import java.nio.file.Paths;

public class FileExistsExample {
    public static void main(String[] args) {
        System.out.println(doesFileExists("/experimental/users.csv"));
    }

    public static boolean doesFileExists(String path) {
        if (path != null && !path.isEmpty()) {
            return Files.exists(Paths.get(path));
        }
        return false;
    }
}
On By