Here you can find the source of deleteIfExists(Path thePath)
static public void deleteIfExists(Path thePath)
//package com.java2s; /*//from www .j ava 2 s.c om * Copyright (c) 2013 christianr. * All rights reserved. This program and the accompanying materials * are made available under the terms of the GNU Lesser Public License v3 * which accompanies this distribution, and is available at * http://www.gnu.org/licenses/lgpl-3.0.html * * Contributors: * christianr - initial API and implementation */ import java.io.IOException; import java.nio.file.FileVisitResult; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.SimpleFileVisitor; import java.nio.file.attribute.BasicFileAttributes; public class Main { static public void deleteIfExists(Path thePath) { try { Files.walkFileTree(thePath, new SimpleFileVisitor<Path>() { @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { Files.deleteIfExists(file); return FileVisitResult.CONTINUE; } @Override public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { Files.deleteIfExists(dir); return FileVisitResult.CONTINUE; } }); } catch (Exception e) { throw new RuntimeException(e); } } }