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