Files类的deleteIfExists(Path path)
Files.deleteIfExists方法如果文件删除成功则返回true,如果文件不存在则返回false。如果删除的是一个不为空的目录则抛出java.nio.file.DirectoryNotEmptyException异常,该方法推荐使用,奉上代码。
private static void deleteFile(){
String filePath =System.getProperty("user.dir") + "/" + "test.txt";
boolean deleteIfExists = false;
try {
deleteIfExists = Files.deleteIfExists(Paths.get(filePath));
} catch (IOException e) {
e.printStackTrace();
}
log.info("删除文件"+deleteIfExists);
}