Java如何获取没有全名的文件 [英] Java how to get file without full name

查看:54
本文介绍了Java如何获取没有全名的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从应用程序的另一部分中的应用程序下载的目录中获取文件,但是此文件的名称是动态创建的.我的意思是,它始终是Query_,然后是一些数字,fe. Query_21212121212Query_22412221.我需要获取绝对文件路径,例如C:/dir/Query_ *.该怎么做?

I need to get the file from directory which is downloaded from application in another part of app, but name of this file is creating dynamically. I mean that it is always Query_ and then some digits, fe. Query_21212121212, Query_22412221. I need to get the absolute file path, sth like C:/dir/Query_*. How to do that?

推荐答案

如果知道文件所在的路径,则可以创建DirectoryStream并获取其中的所有文件.

If you know the Path where the files reside, you can create a DirectoryStream and get all the files in it.

请参见 https://docs.oracle.com/javase/tutorial/essential/io/dirs.html#glob

示例(通过上面的链接):

Example (from link above):

Path dir = ...;
try (DirectoryStream<Path> stream =
     Files.newDirectoryStream(dir, "*.{java,class,jar}")) { //<--- change the glob to 
                                                            //fit your name pattern.
    for (Path entry: stream) {
        System.out.println(entry.getFileName());
    }
} catch (IOException x) {
    // IOException can never be thrown by the iteration.
    // In this snippet, it can // only be thrown by newDirectoryStream.
    System.err.println(x);
}

关于glob: https://docs.oracle. com/javase/tutorial/essential/io/fileOps.html#glob

因此,在您的情况下,可能类似于"Query_*".您使用特殊扩展名还是根本不使用扩展名?如果是.txt,则全局名称应类似于"Query_[0-9]+.txt".

So in your case it would probably be something like "Query_*". Do you use a special extension or no extension at all? If it were .txt then the glob should be like "Query_[0-9]+.txt".

这篇关于Java如何获取没有全名的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆