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?
Asked
Active
Viewed 523 times
1

kamilEsz
- 13
- 2
-
are there more files with that name? – XtremeBaumer Apr 07 '17 at 07:33
-
no, only one file – kamilEsz Apr 07 '17 at 07:34
-
do you know the path? to the directory? – XtremeBaumer Apr 07 '17 at 07:38
-
These links may help: https://docs.oracle.com/javase/tutorial/essential/io/dirs.html#listdir and https://docs.oracle.com/javase/tutorial/essential/io/dirs.html#glob – Fildor Apr 07 '17 at 07:45
1 Answers
1
If you know the Path where the files reside, you can create a DirectoryStream and get all the files in it.
See 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);
}
About globs: https://docs.oracle.com/javase/tutorial/essential/io/fileOps.html#glob
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"
.

Fildor
- 14,510
- 4
- 35
- 67