Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Grails/Groovy文件删除_Java_File_Grails_Groovy_Delete File - Fatal编程技术网

Java Grails/Groovy文件删除

Java Grails/Groovy文件删除,java,file,grails,groovy,delete-file,Java,File,Grails,Groovy,Delete File,我很难删除文件。我会告诉你什么是有效的,如果这是可以接受的,你可以担任法官 class StupidService{ def doThings(){ def tmpDirString = "dumpit" def currentDir = new File("../${tempDirString}") currentDir.eachFile(FileType.FILES){ def f=it def answer = SuperComplicatedService.d

我很难删除文件。我会告诉你什么是有效的,如果这是可以接受的,你可以担任法官

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”等等,然后最终删除文件


发生了什么事?

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


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

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

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