memostack
article thumbnail
블로그를 이전하였습니다. 2023년 11월부터 https://bluemiv.tistory.com/에서 블로그를 운영하려고 합니다. 앞으로 해당 블로그의 댓글은 읽지 못할 수 도 있으니 양해바랍니다.
반응형

파일이 존재하는지 확인

Java에서 파일이 존재하는지 확인하기 위해서는 File 오브젝트의 exists() 메소드를 사용한다.

import java.io.File;

public class App 
{
    public static void main( String[] args ) {
        // 파일의 경로
        final File driverFile = new File("src/resources/bin/chromedriver.exe");
        final String driverFilePath = driverFile.getAbsolutePath();
        
        // 파일이 존재하는지 확인
        if(!driverFile.exists()) {
            // 파일이 존재하지 않는다면, 오류 발생.
            throw new RuntimeException("Not found file. <" + driverFilePath + ">");
        }
    }
}

 

대상 파일이 파일인지 확인

파일인지 디렉토리인지 확인을 할때는 isFile() 메소드를 사용한다.

import java.io.File;

public class App 
{
    public static void main( String[] args ) {
        // 파일 경로
        final File driverFile = new File("src/main/resources/bin/chromedriver.exe");
        final String driverFilePath = driverFile.getAbsolutePath();
        
        // 파일인지 디렉토리인지 확인
        if(!driverFile.isFile()) {
            throw new RuntimeException("This is not file. <" + driverFilePath + ">");
        }
    }
}

 

대상 파일이 디렉토리인지 확인

반대로, 디렉토리인지 확인할 때는 isDirectory() 메소드를 사용한다.

import java.io.File;

public class App 
{
    public static void main( String[] args ) {
        // 파일 경로
        final File driverFile = new File("src/main/resources/bin/chromedriver.exe");
        final String driverFilePath = driverFile.getAbsolutePath();
        
        // 파일인지 디렉토리인지 확인
        if(!driverFile.isDirectory()) {
            throw new RuntimeException("This is not directory. <" + driverFilePath + ">");
        }
    }
}
반응형
블로그를 이전하였습니다. 2023년 11월부터 https://bluemiv.tistory.com/에서 블로그를 운영하려고 합니다. 앞으로 해당 블로그의 댓글은 읽지 못할 수 도 있으니 양해바랍니다.
profile

memostack

@bluemiv_mm

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!