File exists, cant be read giving path in standard way, identical strings don't match - Groovy
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?
def rootPath = "x:/"
files.each { f ->
File xmlFile = new File( rootPath, f )
String filename = xmlFile.name
File parentDir = xmlFile.parent
parentDir.list().each { f2 ->
// Does this still print |PreUpload_140111-192158.xml|PreUpload_140111-192158.xml|
println "|$f2|$fileName|"
// Does this still print false?
println( f2 == fileName )
boolean canRead = new File( parentDir, f2 ).canRead()
// still prints true ?
println "Can Read : $canRead"
}
}
[Edit]
So, it would appear that the issue was CR
characters at the end of the Strings in the files
collection
Not sure how the variable files
is populated, but something somewhere needs a trim()
;-)
精彩评论