본문으로 바로가기
반응형
파일 유틸을 가지고 뭔가 쪼물딱 뚝딱 만들때 가장 많이 쓰이는 것 중 하나입니다.

일단 파일이 없으면 exception 을 던져버리면 되긴 하지만, 파일이 존재하는지 없는지 여부에 따라 뭔가 로직을 구현해야 할 때도 있기에 유용합니다.

import java.io.File;

public class FileExists {
public static boolean checkFileExists(String filePath) {
File file = new File(filePath);
return file.exists();
}
}

boolean 이기에 True, False 를 리턴합니다.

같은방법이지만, True False를 String 으로 리턴하고 싶을때,


import java.io.File;

public class FileExists {
public static String checkFileExists(String filePath) {
File file = new File(filePath);
if (file.exists()) {
return "True";
} else {
return "False";
}
}
}


Random Photo

반응형