Java java.nio.file.Files deleteIfExists() Method
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
path: The path of the file or directory to delete (Pathobject)
Return Value
true: If the file exists and is successfully deletedfalse: If the file does not exist
: Generic E (returns the last element in the list)
IOException: If an I/O error occursSecurityException: If a security manager exists and denies delete access
Use Cases
deleteIfExists() The method is particularly suitable for the following scenarios:
- Need to safely delete a file, whether or not it exists
- Do not want an exception thrown because the file does not exist
- 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
- Directory deletion restrictions: Can only delete empty directories; if the directory is not empty, it throws
DirectoryNotEmptyException - Symbolic links: When deleting a symbolic link, only the link itself is deleted and the target file is not affected
- File locking: If the file is locked by another process, the delete operation may fail
- 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
- Always check the return value to know whether the operation succeeded
- Properly handle the possible IOException
- For directory deletion, first ensure the directory is empty
- 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 java.nio.file.Files