在使用代码读取目录的适合还是正常,但到了删除的时候就出现了报错

@Test
    public void main3() throws IOException {
        //不是空文件夹删不掉 DirectoryNotEmptyException
//        Files.delete(Paths.get("C:\\Users\\deletest"));
        Files.walkFileTree(Paths.get("C:\\Users\\deletest"),new SimpleFileVisitor<Path>(){

            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
//                System.out.println(file);
                Files.delete(file);
                return super.visitFile(file, attrs);
            }

            @Override
            public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
                Files.delete(dir);
//                System.out.println("<=========退出"+dir);
                return super.postVisitDirectory(dir, exc);
            }
        });
    }

 代码应该是没有问题的,在上网搜的时候,都是网络传输方面,但我这暂时只是本地,到这里我恍然大悟,删除东西要权限,我不是管理员权限运行的Idea!

可惜在使用管理员权限运行,还是这个报错,那说明不是这个原因。

仔细看了一下报错的路径,发现.git文件夹为隐藏

 点掉隐藏再试试,发现还是不对,但在我手动删了这个.git文件夹后就可以了。

被删库跑路了。为了搞清楚是什么问题,我再复原一下这个deletest文件夹。

好吧,在我几次测试下,确实是因为这个.git的问题导致报错,其他文件夹都是可以正常删除,如果有大佬知道的话,麻烦告诉我一下。等我下次搞清楚了,我也会更新一下。

Logo

技术共进,成长同行——讯飞AI开发者社区

更多推荐