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?
- are there more files with that name?XtremeBaumer– XtremeBaumer2017-04-07 07:33:39 +00:00Commented Apr 7, 2017 at 7:33
- no, only one filekamilEsz– kamilEsz2017-04-07 07:34:17 +00:00Commented Apr 7, 2017 at 7:34
- do you know the path? to the directory?XtremeBaumer– XtremeBaumer2017-04-07 07:38:56 +00:00Commented Apr 7, 2017 at 7:38
- These links may help: docs.oracle.com/javase/tutorial/essential/io/dirs.html#listdir and docs.oracle.com/javase/tutorial/essential/io/dirs.html#globFildor– Fildor2017-04-07 07:45:36 +00:00Commented Apr 7, 2017 at 7:45
Add a comment |
1 Answer
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".