Skip to content
Related Articles

Related Articles

Improve Article

Files delete() method in Java with Examples

  • Difficulty Level : Easy
  • Last Updated : 30 Jul, 2019

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:

  1. NoSuchFileException – if the file does not exist (optional specific exception)
  2. DirectoryNotEmptyException – if the file is a directory and could not otherwise be deleted because the directory is not empty
  3. IOException – if an I/O error occurs
  4. 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 delete?(Path) method:
Program 1:




// 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 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)

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :