Files.deleteIfExists(Path path) has the following syntax.
public static boolean deleteIfExists(Path path) throws IOException
In the following code shows how to use Files.deleteIfExists(Path path) method.
/*from w w w.j a v a 2s .com*/ 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.