Delete a file using Java

Java provides methods to delete files using java programs. On the contrary to normal delete operations in any operating system, files being deleted using java program is deleted permanently without being moved to trash/recycle bin.
Following are the methods used to delete a file in Java:

  1. Using java.io.File.delete() function: Deletes the file or directory denoted by this abstract path name.
    Syntax:

    public boolean delete()
    Returns: true if and only if the file or 
    directory is successfully deleted; false otherwise
    // Java program to delete a file 
    import java.io.*;
    
    public class Test
    {
    	public static void main(String[] args)
    	{
    		File file = new File("C:\\Users\\Mayank\\Desktop\\1.txt");
    		
    		if(file.delete())
    		{
    			System.out.println("File deleted successfully");
    		}
    		else
    		{
    			System.out.println("Failed to delete the file");
    		}
    	}
    }
    

    Output:

    File deleted successfully
  2. Using java.nio.file.files.deleteifexists(Path p) method defined in Files package: This method deletes a file if it exists. It also deletes a directory mentioned in the path only if the directory is not empty.
    Syntax:
    public static boolean deleteIfExists(Path path) throws IOException
    Parameters: path - the path to the file to delete
    Returns: true if the file was deleted by this method; 
    false if the file could not be deleted because it did not exist.
    Throws: 
    DirectoryNotEmptyException - if the file is a directory and 
    could not otherwise be deleted because the directory is not empty
    (optional specific exception)
    IOException - if an I/O error occurs
    // Java program to demonstrate delete using Files class
    import java.io.IOException;
    import java.nio.file.*;
    
    public class Test
    {
    	public static void main(String[] args)
    	{
    		try
    		{
    			Files.deleteIfExists(Paths.get("C:\\Users\\Mayank\\Desktop\\
                445.txt"));
    		}
    		catch(NoSuchFileException e)
    		{
    			System.out.println("No such file/directory exists");
    		}
    		catch(DirectoryNotEmptyException e)
    		{
    			System.out.println("Directory is not empty.");
    		}
    		catch(IOException e)
    		{
    			System.out.println("Invalid permissions.");
    		}
    		
    		System.out.println("Deletion successful.");
    	}
    }
    
    

    Output:

    Deletion successful.

Reference:

This article is contributed by Mayank Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

GATE CS Corner    Company Wise Coding Practice

Recommended Posts:







Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.