V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
heavyrainn
V2EX  ›  程序员

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

  •  
  •   heavyrainn · 2021-08-11 19:49:31 +08:00 · 1196 次点击
    这是一个创建于 978 天前的主题,其中的信息可能已经有所发展或是发生改变。

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

    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

    3 条回复    2021-08-13 16:56:12 +08:00
    AoEiuV020
        1
    AoEiuV020  
       2021-08-11 20:24:21 +08:00
    这人工都没法分辨吧,你的 path 是整个完整路径,
    /test/trans/test.jar
    /test/trans/content/ojdbc6.jar
    对 glob 来说都是带 /的子目录里,都要**,
    你这 location 没用上,
    AoEiuV020
        2
    AoEiuV020  
       2021-08-11 21:05:34 +08:00   ❤️ 1
    现学现卖,不贴代码了,
    heavyrainn
        3
    heavyrainn  
    OP
       2021-08-13 16:56:12 +08:00
    @AoEiuV020 啊…我明白我这个错在哪里了,谢谢谢谢
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5585 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 22ms · UTC 08:00 · PVG 16:00 · LAX 01:00 · JFK 04:00
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.