티스토리 뷰

Java/기초

[java] File의 다양한 메서드

hi_hannah 2020. 12. 6. 00:38

java.io.File

메서드 설명
boolean canRead() 읽을 수 있는 파일인지 검사한다.
boolean canWrite() 쓸 수 있는 파일인지 검사한다
boolean canExecute() 실행할 수 있는 파일인지 검사한다
int compareTo(File pathName) 지정한 파일(pathname)과 비교한다
boolean exist() 파일이 존재하는지 검사한다.
boolean isAbsolute() 파일 또는 디렉토리가 절대경로명으로 지정되었는지 확인한다
boolean isDirectory() 디렉토리인지 확인한다
boolean isFile() 파일인지 확인한다.
boolean createNewFile() 아무런 내용이 없는 새로운 파일을 생성한다. (단, 이미 생성하려는 파일이 이미 존재하면 생성되지 않는다.)
static File createTempFile(String prefix, String suffix) 임시파일을 시스템의 지정된 디렉토리에 생성한다
boolean delete() 파일을 삭제한다
void deleteOnExit 응용 프로그램 종료시 파일을 삭제한다
boolean equals(Object obj) 주어진 객체가 같은 파일인지 비교한다
long length() 파일의 크기를 반환한다
String[] list() 디렉토리의 파일목록(디렉토리 포함)을 String 배열로 반환한다.
String[] list(FilenameFilter filter) FileNameFilter 인스턴스에 구현된 조건에 맞는 파일을 String 배열로 반환한다.
File[] listFiles() 디렉토리의 파일목록(디렉토리 포함)을 File배열로 반환한다.
File[] listFiles(FilenameFilter filter) 디렉토리의 파일목록(디렉토리 포함)에서 Filter의 조건과 일치하는 파일들을 File 배열로 반환한다.
Path toPath() 파일을 Path로 변환해서 반환한다
URI toURI() 파일을 URI로 반환해서 반환한다
java-practice
ㄴsrc
    ㄴtest
        ㄴfile
            ㄴFileExample.java
    ㄴfacebook.png
public class FileExample {
    public static void main(String[] args) throws Exception {
    File f = new File("C:\\Spring\\java-practice\\src\\facebook.png");

    System.out.println("canRead : " + f.canRead());
    System.out.println("canWrite : " + f.canWrite());
    System.out.println("canExecute : " + f.canExecute());
    System.out.println("isAbsolute : " + f.isAbsolute());
    System.out.println("getPath : " + f.getPath());
    System.out.println("lengh : " + f.length());
    System.out.println("file exist? : " + f.exists());
    System.out.println("directory? : "+ f.isDirectory());

    }
}
canRead : true
canWrite : true
canExecute : true
isAbsolute : true
getPath : C:\Spring\java-practice\src\facebook.png
lengh : 977
file exist? : true
directory? : false

참고자료

댓글