1

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?

kamilEsz
  • 13
  • 2

1 Answers1

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