8

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

 3 years ago
source link: https://www.v2ex.com/t/795167
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

V2EX  ›  程序员

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

  heavyrainn · 22 小时 3 分钟前 · 456 次点击

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

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


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK