在JAVA中查找来自计算机所有驱动器的名称(任何扩展名)的文件

时间:2014-09-23 09:21:09

标签: java

任何人都可以解释我,我怎样才能从电脑中找到特定的文件?

enter image description here

当我点击搜索按钮时,输出应该显示包含任何扩展名的示例文件的所有路径。

请帮帮我。提前谢谢你。

1 个答案:

答案 0 :(得分:0)

java.nio.file包为此有用功能提供了编程支持。每个文件系统实现都提供PathMatcher。您可以使用FileSystem类中的getPathMatcher(String)方法检索文件系统的PathMatcher。以下代码段获取默认文件系统的路径匹配器:

String pattern = ...;
PathMatcher matcher =
    FileSystems.getDefault().getPathMatcher("glob:" + pattern);

并创建PathMatcher实例,如 -

PathMatcher matcher =
    FileSystems.getDefault().getPathMatcher("glob:*.{java,class}");

Path filename = ...;
if (matcher.matches(filename)) {
    System.out.println(filename);
}

请参阅此示例。 (要获得路径,你必须获得PathMatcher的相对路径。)

http://docs.oracle.com/javase/tutorial/essential/io/find.html

更新 -

对于我的电脑,你必须逐个循环通过所有驱动器,并以驱动器号作为路径,D:\将成为你的路径。上面的代码适用于特定路径。