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
}