2011-06-16 81 views
1

我有一個艱難的時間刪除文件。我會告訴你什麼是工作,如果這是可以接受的,你可以成爲法官。Grails/Groovy文件刪除

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」等等,最後刪除該文件。

這是怎麼回事?

回答

3

SuperComplicatedService.doStuff(f)是做什麼用的?如果它打開文件,確保它在返回之前關閉它。否則,在垃圾收集器收集引用它的對象之前,您將無法刪除該文件。

I can't delete a file in java

2

代碼在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 
    } 
刪除該文件的文件和文件夾