Grails/Groovy 文件删除

发布于 2024-11-15 10:11:26 字数 847 浏览 1 评论 0原文

我在删除文件时遇到了困难。我将向您展示什么是有效的,您可以判断这是否可以接受。

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 行的“这不是一个笑话:64457”等,然后最终删除该文件。

到底是怎么回事?

I am having a tough time deleting a file. I will show you what is working, and you can be the judge if this is acceptable.

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++
    }

  }
}
}

And so this prints out between 40k and 150k lines of "This is not a joke: 64457" etc. and then finally deletes the file.

What is going on?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

忘羡 2024-11-22 10:11:26

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

请参阅我无法删除java中的文件

What does SuperComplicatedService.doStuff(f) do? If it opens the file, make sure it closes it before returning. Otherwise, you won't be able to delete the file until the garbage collector collects the object that references it.

See I can't delete a file in java

单挑你×的.吻 2024-11-22 10:11:26

Groovy/Grails 中删除文件及其文件夹的代码

    String filePath = "c:/dir"+"/"+"carpeta"+"/"+documentoInstance.nombreArchivo
    String folderPath = "c:/dir"+"/"+"carpeta"+"/"
    boolean fileSuccessfullyDeleted =  new File(filePath).delete()
    boolean folderSuccessDeleted = new File(folderPath).deleteDir()
    if(fileSuccessfullyDeleted && folderSuccessDeleted){
        documentoInstance.delete flush:true
    }
    else{
        flash.error = "Archivo no borrado."
        return
    }

Code for delete a file and folder of the file in Groovy/Grails

    String filePath = "c:/dir"+"/"+"carpeta"+"/"+documentoInstance.nombreArchivo
    String folderPath = "c:/dir"+"/"+"carpeta"+"/"
    boolean fileSuccessfullyDeleted =  new File(filePath).delete()
    boolean folderSuccessDeleted = new File(folderPath).deleteDir()
    if(fileSuccessfullyDeleted && folderSuccessDeleted){
        documentoInstance.delete flush:true
    }
    else{
        flash.error = "Archivo no borrado."
        return
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文