JAVA/NIO2

[NIO.2]Deleting a File or Directory

lahuman 2013. 11. 25. 15:14
728x90

파일 및 디렉토리 삭제


Files class에서는 2개의 삭제 Method를 제공한다.


1. delete(Path)

 delete(Path) 는 삭제 실패시 Exception을 던진다. 예를 들면 파일이 없을 경우 NoSuchFileException Exception을 던진다.

try {
    Files.delete(path);
} catch (NoSuchFileException x) {
    System.err.format("%s: no such" + " file or directory%n", path);
} catch (DirectoryNotEmptyException x) {
    System.err.format("%s not empty%n", path);
} catch (IOException x) {
    // File permission problems are caught here.
    System.err.println(x);
}

2. deleteIfExists(Path) 

 deleteIfExists(Path) 역시 파일을 삭제 한다. 그러나 존재 하지 않을 경우 Exception을 던지지 않는다. Exception을 던지지 않고 싶다면 이 Method를 사용 하면 된다.


참조 : http://docs.oracle.com/javase/tutorial/essential/io/delete.html

728x90