I have a very frustrating problem reading files in Groovy (windows). I’ve spent quite a bit of time trying to track down the root cause. However I have boiled it down to two identical file names not matching, so i’m bamboozled!
Here’s some diagnostic code and the results:
def rootPath = "x:/"
def filePath
files.each
{
filePath = rootPath + it
File xmlFile = new File(filePath)
println xmlFile.canRead() //returns : false
println xmlFile.exists() //returns : false
xmlFile = new File(new File(filePath).getParent() + "/" + new File(filePath).getName().toString())
println xmlFile.canRead() //returns : false
String fileName = new File(filePath).getName()
String parentDir = new File(filePath).getParent()
new File(parentDir).list().each
{
println "|" + it + "|" + fileName + "|"
//returns |PreUpload_140111-192158.xml|PreUpload_140111-192158.xml|
println it.toString().equals(fileName)
//returns false!!
println "Can Read : " + new File(parentDir + "/" + it.toString()).canRead()
//returns true
}
}
Do you get the same results with this cleaned up code?
[Edit]
So, it would appear that the issue was
CRcharacters at the end of the Strings in thefilescollectionNot sure how the variable
filesis populated, but something somewhere needs atrim()😉