Files delete() method in Java with Examples Last Updated : 14 Apr, 2023 Comments Improve Suggest changes Like Article Like Report 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 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 deleted 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 void delete(Path path) throws IOException Parameters: This method accepts a parameter path which is the path to the file to delete. Return value: This method returns nothing. Exception: This method will throw following exceptions: NoSuchFileException - if the file does not exist (optional specific exception)DirectoryNotEmptyException - if the file is a directory and could not otherwise be deleted because the directory is not emptyIOException - if an I/O error occursSecurityException - 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 delete(Path) method: Program 1: Java // Java program to demonstrate // java.nio.file.Files.delete() 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"); // delete File try { Files.delete(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 deleted from the path "D:\\Work\\Test\\file1.txt" Program 2: Java // Java program to demonstrate // java.nio.file.Files.delete() method import java.io.IOException; import java.nio.file.*; public class GFG { public static void main(String[] args) { // create object of Path Path pathOfFile1 = Paths.get("D:\\temp\\Files" + "\\Cover Letter.docx"); Path pathOfFile2 = Paths.get("D:\\temp\\Files" + "\\Java-Concurrency-Essentials.pdf"); // delete both Files try { Files.delete(pathOfFile1); Files.delete(pathOfFile2); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } Output: Before deleting the file: After deleting the file: References: https://docs.oracle.com/javase/10/docs/api/java/nio/file/Files.html#delete(java.nio.file.Path) Comment More infoAdvertise with us Next Article Files 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 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 deleteIfExists() method in Java with Examples 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 3 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 Files size() method in Java with Examples size() method of java.nio.file.Files help us to get the size of a file (in bytes). This method returns the file size, in bytes by taking the path of the file as a parameter. The size may differ from the actual size on the file system due to compression, support for sparse files, or other reasons. Th 2 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 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 isFile() method in Java with Examples The isFile() function is a part of File class in Java. This function determines whether the is a file or Directory denoted by the abstract filename is File or not. The function returns true if the abstract file path is File else returns false. Function signature: public boolean isFile() Syntax: file 2 min read FileInputStream close() Method in Java with Examples FileInputStream class is helpful to read data from a file in the form of a sequence of bytes. FileInputStream is meant for reading streams of raw bytes such as image data. For reading streams of characters, consider using FileReader. FileInputStream.close() method After any operation to the file, w 2 min read File length() method in Java with Examples The length() function is a part of File class in Java . This function returns the length of the file denoted by the this abstract pathname was length.The function returns long value which represents the number of bytes else returns 0L if the file does not exists or if an exception occurs. Function s 2 min read Like