如果文件已经存在,如何通过附加一些递增的数字递归检查和重命名文件?
我写了下面的函数,但它给了我一个例外
org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'E:\Projects\repo1\in_conv1.xml' with class 'java.lang.String' to class 'java.io.File'
代码
//newFilePath = E:\Projects\repo1\old\testcustprops.xml
String newFilePath = checkForExistenceAndRename(newFilePath,false)
private String checkForExistenceAndRename(String newFilePath, boolean flag){
File f = new File(newFilePath)
if(!flag){
if(f.exists()){
//renaming file
newFilePath=newFilePath[0..-5]+"_conv${rename_count++}.xml"
f = checkForExistenceAndRename(newFilePath,false)
}
else
f = checkForExistenceAndRename(newFilePath,true)
}
else
return newFilePath
}