파일과 디렉토리 존재 여부 확인하기

렉토리 모두 File 클래스의 exists()을 호출하면 알 수 있습니다.

파일, 디렉토리 구분법

파일인지 디렉토리인지 어떻게 알 수 있을까요?

File 클래스에 isFile(), isDirectory() 함수가 존재합니다

File fileOrDirectory = new File("example.txt");
if (fileOrDirectory.isFile()) {
// 파일인 경우
} else if (fileOrDirectory.isDirectory()) {
// 디렉토리인 경우
}

java.nio.file

java.nio.file 클래스를 이용할 수도 있습니다.

Path path = Paths.get("example.txt");
boolean exists = Files.exists(path);
boolean isDirectory = Files.isDirectory(path);
boolean isRegularFile = Files.isRegularFile(path);

Leave a Comment