2017-04-25 46 views
-1

症状很简单:Windows拒绝删除一个目录,因为它认为它的内容仍然存在 - 虽然递归删除只是删除它们。所以我的第一个猜测是在删除它的内容之后并在删除它之前,父目录上出现了冲突/同步或类似的缺失。没有可靠的递归目录删除可能吗?

我试着公地IO 2.5版FileUtils.deleteDirectory和FileUtils.cleanDirectory功能,以及我自己的简化测试:

@Test 
public void testMySimpleDelete() throws IOException, InterruptedException { 
    File dir = TEST_DIR; 
    for (int i = 0; i < 10; i++) { 
     dir = new File(dir, Integer.toString(i)); 
    } 

    for (int i = 0; i < 1000; i++) { 
     LOG.info(""+i); 
     assertTrue("loop #" + i, dir.mkdirs()); 
     mySimpleDelete(Paths.get(TEST_DIR.getAbsolutePath())); 
     assertFalse(TEST_DIR.exists()); 
    } 
} 

private void mySimpleDelete(Path file) throws IOException, InterruptedException { 
    Files.walkFileTree(file, new FileVisitor<Path>() { 
     @Override 
     public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { 
      return FileVisitResult.CONTINUE; 
     } 

     @Override 
     public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { 
      throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. 
     } 

     @Override 
     public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException { 
      throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. 
     } 

     @Override 
     public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { 
      //LOG.info(dir.toString()); 
      Files.delete(dir); 
      return FileVisitResult.CONTINUE; 
     } 
    }); 
} 

无不显示出相同的结果/症状。

如果你想自己验证这个行为(仅限于Windows!Linux根本不锁定任何东西,并且行为方式更加宽松),只需检查https://github.com/jjYBdx4IL/java-evaluation并运行“mvn test -Dtest = org.apache .commons.io.FileUtilsTest”。

+0

参见http://stackoverflow.com/q/22948189/886887 –

回答

1

问题是TortoiseGIT 再次。上次它非常愚蠢,没有配置其打包的git.exe并使用我的cygwin安装中的一个,这导致了问题并浪费了我的时间。

现在,在这种情况下,状态缓存,用来显示Windows资源管理器中的状态图标,甚至扫描混帐忽略构建和测试目录,从而锁定的东西在里面...... OMG。太糟了。

解决方案:

TortoiseGIT -> Settings -> Icon Overlays -> Status Cache set to None