简体   繁体   中英

Groovy File List behaviour for prefix 'file:'

Seems that under recent groovy, the java File object knows that directory paths specified starting with a 'file:' prefix exist, but won't list files from them. Here's a concise example from groovyConsole:

File f1 = new File("/Users/michael/input")
println "f1 exists: ${f1.exists()}"
println "f1 list: ${f1?.listFiles()}"
File f2 = new File("file:/Users/michael/input")
println "f2 exists: ${f2.exists()}"
println "f2 list: ${f2?.listFiles()}"

f1 listFiles() works fine, f2 listFiles returns an empty list.

I encountered this debugging why spring-integration (upgraded to 4.0.3) suddenly stopped working with grails (upgraded to 2.4.3).

The spring-integration sample code uses the 'file:' prefix. That code works fine under maven/java builds, but add that same sample code to a vanilla grails app and it no longer works.

Any explanation?

As far as I can tell Groovy doesn't do any magic when constructing a new File . This is standard JDK behavior. To use file: you must pass an URI to new File(...) and not a String .

new File("/Users/nobody/Desktop").absolutePath
===> /Users/nobody/Desktop

new File("file:/Users/nobody/Desktop").absolutePath
===> /Users/nobody/Development/Grails/String/file:/Users/nobody/Desktop
     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
     this is the path to my grails test project

new File("file:/Users/nobody/Desktop".toURI ()).absolutePath
===> /Users/nobody/Desktop

Maybe you have a file named file:/Users/michael/input (including the "file:") in your working directory.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM