【发布时间】:2016-10-12 08:29:51
【问题描述】:
我想知道是否以及如何在路径定义中使用通配符。 我想深入一个文件夹并尝试使用 * 但这不起作用。
我想访问随机文件夹中的文件。文件夹结构是这样的:
\test\orig\test_1\randomfoldername\test.zip
\test\orig\test_2\randomfoldername\test.zip
\test\orig\test_3\randomfoldername\test.zip
我尝试了什么:
File input = new File(origin + folderNames.get(i) + "/*/test.zip");
File input = new File(origin + folderNames.get(i) + "/.../test.zip");
提前谢谢你!
【问题讨论】:
-
你的意思是1个文件夹更深,你需要指定你想去哪个文件夹。
-
为了清晰起见会尝试和编辑
-
@conscells 似乎我的问题是我不能将目录流用作文件路径:
Path path = FileSystems.getDefault().getPath(origin + folderNames.get(i)); DirectoryStream<Path> stream = Files.newDirectoryStream(path, "/*/test.zip"); File input = new File(stream);由于流不是字符串,我无法让文件工作。 -
@Warweedy 你必须学会在 api 文档中更加努力。 :) 如果其他人有类似的问题。:docs.oracle.com/javase/7/docs/api/java/nio/file/… 显示如何从流中获取
Path。从Path,您可以使用toFile()检索File。文档是您的朋友。
标签: java file path wildcard filepath