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

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

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

Java File Java java.nio.file.Files


java.nio.file.Files.delete() is a static method provided in the Java NIO (New I/O) package for deleting files or empty directories. The method belongs to java.nio.file package and is part of the file system operation API introduced in Java 7.

Method Syntax

public

static

void

delete

(

Path path

)

throws

IOException

Parameter Description

Return Value

The method has no return value (void type)

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


Method Characteristics

Deletion behavior

Difference from File.delete()

Compared with traditional java.io.File.delete() method,Files.delete() Provides more detailed exception information, which helps developers better handle deletion failures.


Usage examples

Basic Usage

Example

import

java.nio.file.*

;

public

class

FileDeleteExample

{

   

public

static

void

main

(

String

[

]

args

)

{

        Path path

=

Paths.

get

(

"example.txt"

)

;

       

       

try

{

            Files.

delete

(

path

)

;

           

System

.

out

.

println

(

"File deleted successfully"

)

;

       

}

catch

(

NoSuchFileException e

)

{

           

System

.

out

.

println

(

"File does not exist: "

path

)

;

       

}

catch

(

DirectoryNotEmptyException e

)

{

           

System

.

out

.

println

(

"Directory is not empty: "

path

)

;

       

}

catch

(

IOException

e

)

{

           

System

.

out

.

println

(

"Error occurred while deleting file: "

e.

getMessage

(

)

)

;

       

}

   

}

}

Delete directory

Example

import

java.nio.file.*

;

public

class

DirectoryDeleteExample

{

   

public

static

void

main

(

String

[

]

args

)

{

        Path dirPath

=

Paths.

get

(

"empty_directory"

)

;

       

       

try

{

            Files.

delete

(

dirPath

)

;

           

System

.

out

.

println

(

"Directory deleted successfully"

)

;

       

}

catch

(

DirectoryNotEmptyException e

)

{

           

System

.

out

.

println

(

"Cannot delete non-empty directory: "

dirPath

)

;

           

System

.

out

.

println

(

"Please delete the contents of the directory first"

)

;

       

}

catch

(

IOException

e

)

{

           

System

.

out

.

println

(

"Error occurred while deleting directory: "

e.

getMessage

(

)

)

;

       

}

   

}

}


Best Practices

Check whether the file exists before deleting

Example

Path path

=

Paths.

get

(

"file_to_delete.txt"

)

;

if

(

Files.

exists

(

path

)

)

{

   

try

{

        Files.

delete

(

path

)

;

   

}

catch

(

IOException

e

)

{

        e.

printStackTrace

(

)

;

   

}

}

Handling symbolic links

delete() The method deletes the symbolic link itself, not the target file the link points to.

Example

Path linkPath

=

Paths.

get

(

"symbolic_link"

)

;

try

{

    Files.

delete

(

linkPath

)

;

// Only delete the link, do not affect the target file

}

catch

(

IOException

e

)

{

    e.

printStackTrace

(

)

;

}

Use in combination with deleteIfExists()

Java also provides Files.deleteIfExists() method, which silently returns when the file does not exist instead of throwing an exception.

Example

Path path

=

Paths.

get

(

"maybe_existing_file.txt"

)

;

try

{

   

boolean

deleted

=

Files.

deleteIfExists

(

path

)

;

   

if

(

deleted

)

{

       

System

.

out

.

println

(

"File deleted"

)

;

   

}

else

{

       

System

.

out

.

println

(

"File does not exist, no need to delete"

)

;

   

}

}

catch

(

IOException

e

)

{

    e.

printStackTrace

(

)

;

}


Notes

  1. Permission issues: Ensure the program has permission to delete the target file
  2. File locking: If the file is locked by another process, deletion may fail
  3. Cross-Platform Behavior: Different operating systems may implement file deletion differently
  4. Atomicity: The delete operation is atomic, but may not take effect immediately on some file systems
  5. Resource management: The delete operation does not automatically close open file streams; you must ensure the file is not in use

Summary

Files.delete() method is a powerful tool in Java NIO for deleting files or empty directories. Compared with traditional File.delete(), it provides more detailed exception information, making error handling more precise. In practical development, it is recommended to combine Files.exists() or directly use Files.deleteIfExists() to write more robust code.

Java File Java java.nio.file.Files