有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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) 个答案

  1. # 1 楼答案

    这个清理过的代码会得到同样的结果吗

    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"
      }
    }
    

    [编辑]

    因此,问题似乎是CR集合中字符串末尾的files字符

    不确定变量files是如何填充的,但某处需要一个trim();-)