Java Files.deleteIfExists(Path path)
Syntax
Files.deleteIfExists(Path path) has the following syntax.
public static boolean deleteIfExists(Path path) throws IOException
Example
In the following code shows how to use Files.deleteIfExists(Path path) method.
//w w w. j a v a 2 s.c o m
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Files;
import java.nio.file.FileSystems;
public class Main {
public static void main(String[] args) {
Path path = FileSystems.getDefault().getPath("C:/tutorial/photos", "Demo.jpg");
//delete if exists
try {
boolean success = Files.deleteIfExists(path);
System.out.println("Delete status: " + success);
} catch (IOException | SecurityException e) {
System.err.println(e);
}
}
}
The code above generates the following result.