删除文件
发布时间:2020-12-14 17:10:29 所属栏目:大数据 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 //----------------------------------------------------------------------------------println new File('/doesnotexist').exists() // = falsepri
以下代码由PHP站长网 52php.cn收集自互联网 现在PHP站长网小编把它分享给大家,仅供参考 //---------------------------------------------------------------------------------- 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 problemFileNames = [] files.each{ f -> if (!f.delete()) problemFileNames += f.name } def delCnt = files.size() - problemFileNames.size() println "Successfully deleted $delCnt of ${files.size()} file(s)" if (problemFileNames) println "Problems file(s): " + problemFileNames.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. //---------------------------------------------------------------------------------- 以上内容由PHP站长网【52php.cn】收集整理供大家参考研究 如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |