Files deleteIfExists() method in Java with Examples Last Updated : 30 Jul, 2019 Summarize Comments Improve Suggest changes Share Like Article Like Report The deleteIfExists() method of java.nio.file.Files help us to delete a file if the file exists at the path. we pass the path of the file as a parameter to this method. This method will return true if the file was deleted by this method; false if the file could not be deleted because it did not exist. If the file is a symbolic link then the symbolic link itself, not the final target of the link, is deleted. If the file is a directory then this method will delete that file only when the directory is empty. In some implementations, a directory has entries for special files or links that are created when the directory is created. In such implementations, a directory is considered empty when only the special entries exist. In such cases, Directories can be delete using this method. On some operating systems it may not be possible to remove a file when it is open and in use by this Java virtual machine or other programs. Syntax: public static boolean deleteIfExists(Path path) throws IOException Parameters: This method accepts a parameter path which is the path to the file to deleted. Return value: This method returns true if the file was deleted by this method; false if the file could not be deleted because it did not exist. Exception: This method will throw following exceptions: DirectoryNotEmptyException - if the file is a directory and could not otherwise be deleted because the directory is not empty IOException - if an I/O error occurs SecurityException - In the case of the default provider, and a security manager is installed, the SecurityManager.checkdelete(String) method is invoked to check to delete access to the file Below programs illustrate deleteIfExists(Path) method: Program 1: Java // Java program to demonstrate // java.nio.file.Files.deleteIfExists() method import java.io.IOException; import java.nio.file.*; public class GFG { public static void main(String[] args) { // create object of Path Path path = Paths.get("D:\\Work\\Test\\file1.txt"); // deleteIfExists File try { Files.deleteIfExists(path); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } Output: Before deleting the file: The file is present in the path "D:\\Work\\Test\\file1.txt" After deleting the file: The file is deleteIfExistsd from the path "D:\\Work\\Test\\file1.txt" Program 2: Java // Java program to demonstrate // java.nio.file.Files.deleteIfExists() method import java.io.IOException; import java.nio.file.*; public class GFG { public static void main(String[] args) { // create an object of Path Path pathOfFile = Paths.get("D:\\Work\\Test\\" + "text1.txt"); // delete both File if file exists try { boolean result = Files.deleteIfExists(pathOfFile); if (result) System.out.println("File is deleted"); else System.out.println("File does not exists"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } Output: References: https://docs.oracle.com/javase/10/docs/api/java/nio/file/Files.html#deleteIfExists(java.nio.file.Path) Comment More infoAdvertise with us Next Article File delete() method in Java with Examples A AmanSingh2210 Follow Improve Article Tags : Java Java-Functions java.nio.file package Java-Files Practice Tags : Java Similar Reads Files delete() method in Java with Examples The delete() method of java.nio.file.Files help us to delete a file located at the path passed as a parameter. This method may not be atomic with respect to other file system operations. If the file is a symbolic link then the symbolic link itself, not the final target of the link, is deleted. If th 3 min read File delete() method in Java with Examples The delete() function is a part of File class in Java . This function deletes an existing file or directory. If the file is deleted then the function returns true else returns false Function signature: public boolean delete() Syntax: boolean var = file.delete(); Parameters: This method does not acce 2 min read Files createTempDirectory() Method in Java with Examples The createTempDirectory() method of java.nio.file.Files Class is used to create a new directory using the given prefix and attributes. The given prefix acts as the name of the formed directory, may be null. The directory is set with given attributes. Based on the type of arguments passed, the Files 4 min read File createNewFile() method in Java with Examples The createNewFile() function is a part of File class in Java . This function creates new empty file. The function returns true if the abstract file path does not exist and a new file is created. It returns false if the filename already exists. Function signature: public boolean createNewFile() Synta 2 min read Files copy() Method in Java with Examples The copy() method of java.nio.file.Files Class is used to copy bytes from a file to I/O streams or from I/O streams to a file. I/O Stream means an input source or output destination representing different types of sources e.g. disk files. Methods: Based on the type of arguments passed, the Files cla 5 min read File canWrite() method in Java with examples The canWrite()function is a part of File class in Java . This function determines whether the program can write the file denoted by the abstract path name.The function returns true if the abstract file path exists and the application is allowed to write the file. Function signature: public boolean c 2 min read Like