You can delete files, directories or links. In the case of a symbolic link, the link is deleted and not the target of the link. In the case of a directory, it must be empty or the delete fails.The
Path
class provides two delete methods.The no-argument
delete
method deletes the file or throws an exception if it fails. You can catch the exception to determine why the delete failed:try { path.delete(); } 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); }The
delete(boolean)
method allows you to specify what should happen when the file does not exist. Passing true causes this method to throw an exception when the file does not exist. Passing false causes the method to do nothing when the file does not exist. Failing silently is useful when you have multiple threads deleting files and you don't want to throw an exception just because one thread got there first.