我很难删除文件。我将向您展示有效的方法,如果可以接受,您可以作为法官。

class StupidService{
def doThings(){
  def tmpDirString = "dumpit"
  def currentDir = new File("../${tempDirString}")
  currentDir.eachFile(FileType.FILES){
    def f=it
    def answer = SuperComplicatedService.doStuff(f)
    //this works, now I need to move the file to the "done" folder
    File dir = new File("../${tempDirString}/done");
    def endupFile = new File("../${tempDirString}/done/${f.name}")
    FileUtils.copyFile(f, endupFile)
    //this works; the file is copied to where I want it successfully; now I just need to delete the initial file.
    def thisIsAJoke=0
    while(f.delete()==false){
      println "This is not a joke: ${thisIsAJoke}"
      thisIsAJoke++
    }

  }
}
}

这样就打印出40k到150k行之间的“这不是一个 Jest :64457”等信息,然后最终删除该文件。

到底是怎么回事?

最佳答案

SuperComplicatedService.doStuff(f)是做什么的?如果打开文件,请确保在返回之前将其关闭。否则,您将无法删除文件,除非垃圾收集器收集了引用该文件的对象。

参见I can't delete a file in java

关于java - Grails/Groovy文件删除,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6365819/

10-11 22:15
查看更多