A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/java/delete-file-using-java/ below:

Delete a File Using Java

Delete a File Using Java

Last Updated : 10 Jan, 2025

Java provides methods to delete files programmatically. In contrast to normal delete operations in any operating system, files being deleted using the Java program are deleted permanently without being moved to the trash/recycle bin. 

Example: A basic program to delete the file from a static path.

Java
// Java program to delete a file
import java.io.*;

public class DeleteMethod 
{
    public static void main(String[] args)
    {
      	// Using Path( Specific File )
        File file = new File("C:\\FileHandling\\Delete\\test.txt");

      	// Delete the File
        if (file.delete()) {
            System.out.println("File deleted successfully");
        }
        else {
            System.out.println("Failed to delete the file");
        }
    }
}

Output:

Methods to Delete File in Java

There are two methods which could help us to remove the File using Java as mentioned below:

  1. java.io.File.delete() Method
  2. java.nio.file.files.deleteifexists(Path p) Method
1. Using java.io.File.delete() Method

This method deletes the file or directory denoted by this abstract path name. 

Syntax: 

public boolean delete()

Returns: It returns true, if and only if the file or the directory is successfully deleted.

Example:

Java
// Delete a file using
// java.io.File.delete() Method
import java.io.*;

public class DeleteMethod 
{
    public static void main(String[] args) throws IOException
    {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        System.out.print("Enter the Path : ");
        
        // Reading File name
        String path = br.readLine();

        File file = new File(path);

        if (file.delete()) {
            System.out.println("File deleted successfully");
        }
        else {
            System.out.println("Failed to delete the file");
        }
    }
}

Output: 

2. Using java.nio.file.files.deleteifexists(Path p) Method

This method deletes a file if it exists. It also deletes a directory mentioned in the path only if the directory is empty. 

Syntax:

public static boolean deleteIfExists(Path path) throws IOException

Throws: 

Example:

Java
// Delete a file using
// java.nio.file.files.deleteifexists(Path p) method
import java.io.*;
import java.nio.file.*;
import java.util.Scanner;

public class DeleteUsingFileClass 
{
    public static void main(String[] args)
    {
      	// Scanning for file path
      	Scanner scn = new Scanner(System.in);
      
      	// Checking the path
        try {
          	String str;
      		str = scn.nextLine();
          
            Files.deleteIfExists(Paths.get(str));
        }
      	// Exceptions
        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.");
        }

      	// Successful Execution
        System.out.println("Deletion successful.");

        scn.close();
    }
}

Output: 



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4