直接上代码吧,直接扒的网上的代码
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
1 AoEiuV020 2021-08-11 20:24:21 +08:00 这人工都没法分辨吧,你的 path 是整个完整路径, /test/trans/test.jar /test/trans/content/ojdbc6.jar 对 glob 来说都是带 /的子目录里,都要**, 你这 location 没用上, |
2 AoEiuV020 2021-08-11 21:05:34 +08:00 ![]() |
3 heavyrainn OP @AoEiuV020 啊…我明白我这个错在哪里了,谢谢谢谢 |