java文件存在,无法以标准方式读取给定路径,相同的字符串与Groovy不匹配
我在Groovy(windows)中读取文件时遇到了一个非常令人沮丧的问题。我花了很多时间试图找出根本原因。然而,我把它归结为两个相同的文件名不匹配,所以我被欺骗了
以下是一些诊断代码和结果:
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
}
}
# 1 楼答案
这个清理过的代码会得到同样的结果吗
[编辑]
因此,问题似乎是
CR
集合中字符串末尾的files
字符不确定变量
files
是如何填充的,但某处需要一个trim()
;-)