Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Java java.nio.file.Files deleteIfExists() Method

Java java.nio.file.Files deleteIfExists() Method

Java File Java java.nio.file.Files


java.nio.file.Files.deleteIfExists() is a utility method in the Java NIO (New Input/Output) package for deleting files or empty directories in the file system. Unlike delete() method,deleteIfExists() , it does not throw an exception when the file does not exist, but returns false.

Method Syntax

public

static

boolean

deleteIfExists

(

Path path

)

throws

IOException

Parameter Description

Return Value

: Generic E (returns the last element in the list)


Use Cases

deleteIfExists() The method is particularly suitable for the following scenarios:

  1. Need to safely delete a file, whether or not it exists
  2. Do not want an exception thrown because the file does not exist
  3. Need to know whether the file was actually deleted (via the return value)

Example Code

Basic Usage

Example

import

java.nio.file.*

;

public

class

DeleteFileExample

{

   

public

static

void

main

(

String

[

]

args

)

{

        Path path

=

Paths.

get

(

"example.txt"

)

;

       

       

try

{

           

boolean

deleted

=

Files.

deleteIfExists

(

path

)

;

           

if

(

deleted

)

{

               

System

.

out

.

println

(

"File deleted successfully"

)

;

           

}

else

{

               

System

.

out

.

println

(

"File does not exist, no need to delete"

)

;

           

}

       

}

catch

(

IOException

e

)

{

           

System

.

err

.

println

(

"Error deleting file: "

e.

getMessage

(

)

)

;

       

}

   

}

}

Delete a directory (must be empty)

Example

Path dirPath

=

Paths.

get

(

"empty_directory"

)

;

try

{

   

boolean

deleted

=

Files.

deleteIfExists

(

dirPath

)

;

   

// Process the result...

}

catch

(

DirectoryNotEmptyException e

)

{

   

System

.

err

.

println

(

"Directory is not empty, cannot delete"

)

;

}

catch

(

IOException

e

)

{

   

System

.

err

.

println

(

"Other I/O error: "

e.

getMessage

(

)

)

;

}


Notes

  1. Directory deletion restrictions: Can only delete empty directories; if the directory is not empty, it throws DirectoryNotEmptyException
  2. Symbolic links: When deleting a symbolic link, only the link itself is deleted and the target file is not affected
  3. File locking: If the file is locked by another process, the delete operation may fail
  4. Permission issues: Sufficient permissions are required to delete the file

Comparison with Other Methods

Method Behavior when the file does not exist Return Value
Files.deleteIfExists() Returns false boolean
Files.delete() Throws NoSuchFileException void
File.delete() (Traditional IO) Returns false boolean

Best Practices

  1. Always check the return value to know whether the operation succeeded
  2. Properly handle the possible IOException
  3. For directory deletion, first ensure the directory is empty
  4. Consider using try-with-resources to handle related resources

Example

try

{

   

if

(

Files.

deleteIfExists

(

path

)

)

{

       

// File exists and was deleted

   

}

else

{

       

// File does not exist

   

}

}

catch

(

IOException

e

)

{

   

// Handle exception

}


Summary

Files.deleteIfExists() provides a safe and convenient way to delete files, especially suitable for scenarios where you are unsure whether the file exists but still need to delete it. Compared with traditional File.delete() method, it provides richer exception information and more flexible NIO path handling capability.

Java File Java java.nio.file.Files