Grails/Groovy file deletion
I am having a tough time deleting a file. I will sh开发者_高级运维ow 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?
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
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
}
精彩评论