Files.delete

2022-08-21 11:30:51 浏览数 (1)

java中我们除了用这种方式删除文件:

代码语言:javascript复制
File file = new File("D:\file\projects\img-comparison-demo\target\generated-sources");
if (file.exists()) {
    try {
        boolean delete = file.delete();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

还可以使用Files工具类:

代码语言:javascript复制
try {
    boolean delete = Files.deleteIfExists(Paths.get("D:\file\projects\img-comparison-demo\target\generated-sources"));
} catch (IOException e) {
    e.printStackTrace();
}

Files还提供了很多的api就不一一列举了,大家自行探索吧

0 人点赞