请教 Java (kotlin)的 glob,玩不转了…两个*的好使,一个*的通通匹配不到…

2021-08-11 19:49:31 +08:00
 heavyrainn

直接上代码吧,直接扒的网上的代码

fun globMatch(glob: String, location: String) : ArrayList<String>{
    val fileList = arrayListOf<String>()
    fileList.clear()
    val pathMatcher: PathMatcher = FileSystems.getDefault().getPathMatcher(glob)
    Files.walkFileTree(Paths.get(location), object : SimpleFileVisitor<Path>() {
        override fun visitFile(
            path: Path,
            attrs: BasicFileAttributes
        ): FileVisitResult {
            println("${path}:${pathMatcher.matches(path)}")
            if (pathMatcher.matches(path)) {
                fileList.add(path.toString())
            }
            return FileVisitResult.CONTINUE
        }

        @Throws(IOException::class)
        override fun visitFileFailed(file: Path, exc: IOException): FileVisitResult {
            return FileVisitResult.CONTINUE
        }
    })
    println(fileList)
    return fileList
}

fun main(args : Array<String>) {
    globMatch(args[1],args[0])
}

运行的结果,当我对 glob 传入**的时候,可以匹配出我想要的,但是会把下层目录的文件也匹配到,当我使用一个*的时候,却完全匹配不到了…结果例如:

[kettle@localhost trans]$ ls -rlt
总用量 88
-rw-rw-r--. 1 kettle kettle 65816 8 月  10 15:42 telnet-0.17-65.el7_8.x86_64.rpm
drwxrwxr-x. 2 kettle kettle  4096 8 月  11 19:47 content
-rw-rw-r--. 1 kettle kettle     0 8 月  11 19:47 test.jar

[kettle@localhost trans]$ java -jar content/superFastCompress-1.0.jar '/test/trans' 'glob:**.jar'
/test/trans/telnet-0.17-65.el7_8.x86_64.rpm:false
/test/trans/content/mysql-connector-java-5.1.48-bin.jar:true
/test/trans/content/postgresql-42.2.23.jar:true
/test/trans/content/mysql-connector-java-5.1.48.jar:true
/test/trans/content/ojdbc6.jar:true
/test/trans/content/superFastCompress-1.0.jar:true
/test/trans/content/TunnelX-1.0-SNAPSHOT.jar:true
/test/trans/test.jar:true
[/test/trans/content/mysql-connector-java-5.1.48-bin.jar, /test/trans/content/postgresql-42.2.23.jar, /test/trans/content/mysql-connector-java-5.1.48.jar, /test/trans/content/ojdbc6.jar, 
/test/trans/content/superFastCompress-1.0.jar, /test/trans/content/TunnelX-1.0-SNAPSHOT.jar, /test/trans/test.jar]
这里就是把我想要的,不想要的都整出来了…我只想要一个 test.jar

[kettle@localhost trans]$ java -jar content/superFastCompress-1.0.jar '/test/trans' 'glob:*.jar'
/test/trans/telnet-0.17-65.el7_8.x86_64.rpm:false
/test/trans/content/mysql-connector-java-5.1.48-bin.jar:false
/test/trans/content/postgresql-42.2.23.jar:false
/test/trans/content/mysql-connector-java-5.1.48.jar:false
/test/trans/content/ojdbc6.jar:false
/test/trans/content/superFastCompress-1.0.jar:false
/test/trans/content/TunnelX-1.0-SNAPSHOT.jar:false
/test/trans/test.jar:false
[]
这里就是啥都没有了…按我想的,本来应该匹配到一个 test.jar 的…

请大家指教,我想用一个*匹配到底应该咋搞 o(╥﹏╥)o

1211 次点击
所在节点    程序员
3 条回复
AoEiuV020
2021-08-11 20:24:21 +08:00
这人工都没法分辨吧,你的 path 是整个完整路径,
/test/trans/test.jar
/test/trans/content/ojdbc6.jar
对 glob 来说都是带 /的子目录里,都要**,
你这 location 没用上,
AoEiuV020
2021-08-11 21:05:34 +08:00
现学现卖,不贴代码了,
heavyrainn
2021-08-13 16:56:12 +08:00
@AoEiuV020 啊…我明白我这个错在哪里了,谢谢谢谢

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/795167

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX