Groovy   发布时间:2022-04-12  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了删除文件大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

下面是大佬教程code.js-code.com 通过网络收集整理的代码片段。

大佬教程小编现在分享给大家,也给大家做个参

//----------------------------------------------------------------------------------
println new File('/doesnotexist').exists()  // => false
println new File('/doesnotexist').delete()  // => false

new File('/createme') << 'Hi there'
println new File('/createme').exists()  // => true
println new File('/createme').delete()  // => true

names = ['file1','file2','file3']
files = names.collect{ new File(it) }
// create 2 of the files
files[0..1].each{ f -> f << f.name }

def deleteFiles(files) {
    def problemFil@R_197_8371@s = []
    files.each{ f ->
        if (!f.delete())
            problemFil@R_197_8371@s += f.name
    }
    def delCnt = files.size() - problemFil@R_197_8371@s.size()
    println "successfully deleted $delCnt of ${files.size()} file(s)"
    if (problemFil@R_197_8371@s)
        println "Problems file(s): " + problemFil@R_197_8371@s.join(',')
}

deleteFiles(files)
// =>
// successfully deleted 2 of 3 file(s)
// Problems file(s): file3

// we can also set files for deletion on exit
tempFile = new File('/xxx')
assert !tempFile.exists()
tempFile << 'junk'
assert tempFile.exists()
tempFile.deleteOnExit()
assert tempFile.exists()
// To confirm this is working,run these steps multiple times in a row.

// Discussion:
// Be careful with deleteOnExit() as there is no way to cancel it.
// There are also mechanisms specifically for creaTing unqiuely named temp files.
// On completion of JSR 203,there will be additional methods available for
// deleting which throw exceptions with detailed error messages rather than
// just return Booleans.
//----------------------------------------------------------------------------------

大佬总结

以上是大佬教程为你收集整理的删除文件全部内容,希望文章能够帮你解决删除文件所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签:删除文件