我很难删除文件。我会告诉你什么是有效的,如果这是可以接受的,你可以成为法官。
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”等,然后最终删除文件。
发生了什么事?
答案 0 :(得分:3)
SuperComplicatedService.doStuff(f)
做什么?如果它打开文件,请确保在返回之前将其关闭。否则,在垃圾收集器收集引用它的对象之前,您将无法删除该文件。
答案 1 :(得分: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
}