[英]How to use a Wildcard in Java filepath
i want to know if and how i could use a Wildcard in a Path definition.我想知道是否以及如何在路径定义中使用通配符。 I want to go one folder deeper and tried using the * but that doesnt work.
我想更深入一个文件夹并尝试使用 * 但这不起作用。
I want to get to files that are in random folders.我想访问随机文件夹中的文件。 Folderstructure is like this:
文件夹结构是这样的:
\test\orig\test_1\randomfoldername\test.zip
\test\orig\test_2\randomfoldername\test.zip
\test\orig\test_3\randomfoldername\test.zip
What i tried:我试过的:
File input = new File(origin + folderNames.get(i) + "/*/test.zip");
File input = new File(origin + folderNames.get(i) + "/.../test.zip");
Thank you in advance!提前谢谢你!
You can use a wildcardard using a PathMatcher:您可以使用 PathMatcher 使用通配符:
You can use a Pattern like this one for your PathMatcher:您可以为您的 PathMatcher 使用这样的模式:
/* Find test.zip in any subfolder inside 'origin + folderNames.get(i)'
* If origin + folderNames.get(i) is \test\orig\test_1
* The pattern will match:
* \test\orig\test_1\randomfolder\test.zip
* But won't match (Use ** instead of * to match these Paths):
* \test\orig\test_1\randomfolder\anotherRandomFolder\test.zip
* \test\orig\test_1\test.zip
*/
String pattern = origin + folderNames.get(i) + "/*/test.zip";
There are details about the syntax of this pattern in the FileSysten.getPathMather method. FileSysten.getPathMather方法中有关于此模式语法的详细信息。 The code to create the PathMather could be:
创建 PathMather 的代码可能是:
PathMatcher pathMatcher = FileSystems.getDefault().getPathMatcher("glob:" + pattern);
You can find all the files that match this pattern using Files.find()
method:您可以使用
Files.find()
方法找到与此模式匹配的所有文件:
Stream<Path> paths = Files.find(basePath, Integer.MAX_VALUE, (path, f)->pathMatcher.matches(path));
The find method returns a Stream<Path>
. find 方法返回一个
Stream<Path>
。 You can do your operation on that Stream or convert it to a List.您可以对该流进行操作或将其转换为列表。
paths.forEach(...);
Or:或者:
List<Path> pathsList = paths.collect(Collectors.toList());
Use the newer Path, Paths, Files使用较新的路径、路径、文件
Files.find(Paths.get("/test/orig"), 16,
(path, attr) -> path.endsWith("data.txt"))
.forEach(System.out::println);
List<Path> paths = Files.find(Paths.get("/test/orig"), 16,
(path, attr) -> path.endsWith("data.txt"))
.collect(Collectors.toList());
Note that the lambda expression with Path path
uses a Path.endsWith
which matches entire names like test1/test.zip
or test.zip
.请注意,带有
Path path
的 lambda 表达式使用Path.endsWith
匹配整个名称,如test1/test.zip
或test.zip
。
16 here is the maximal depth of the directory tree to look in. There is a varargs options parameter, to for instance (not) follow symbolic links into other directories. 16 这里是要查看的目录树的最大深度。有一个可变参数选项参数,例如(不)跟随符号链接进入其他目录。
Other conditions would be:其他条件是:
path.getFileName().endsWith(".txt")
path.getFileName().matches(".*-2016.*\\.txt")
I don't think that it's possible to use wildcard in such way.我认为不可能以这种方式使用通配符。 I propose you to use a way like this for your task:
我建议你使用这样的方式来完成你的任务:
File orig = new File("\test\orig");
File[] directories = orig.listFiles(new FileFilter() {
public boolean accept(File pathname) {
return pathname.isDirectory();
}
});
ArrayList<File> files = new ArrayList<File>();
for (File directory : directories) {
File file = new File(directory, "test.zip");
if (file.exists())
files.add(file);
}
System.out.println(files.toString());
Here is a complete example of how to get a list of files from a give file based on a pattern using the DirectoryScanner implementation provided by Apache Ant.下面是一个完整的示例,说明如何使用 Apache Ant 提供的 DirectoryScanner 实现基于模式从给定文件中获取文件列表。
Maven POM: Maven POM:
<!-- https://mvnrepository.com/artifact/org.apache.ant/ant -->
<dependency>
<groupId>org.apache.ant</groupId>
<artifactId>ant</artifactId>
<version>1.8.2</version>
</dependency>
Java:爪哇:
public static List<File> listFiles(File file, String pattern) {
ArrayList<File> rtn = new ArrayList<File>();
DirectoryScanner scanner = new DirectoryScanner();
scanner.setIncludes(new String[] { pattern });
scanner.setBasedir(file);
scanner.setCaseSensitive(false);
scanner.scan();
String[] files = scanner.getIncludedFiles();
for(String str : files) {
rtn.add(new File(file, str));
}
return rtn;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.