rgamber
rgamber

Reputation: 5849

Opening a file in Java inside a directory specified by wildcard characters

In Java, is it possible to specifiy a directory using a wildcard character, when trying to create a file object as below?

File newFile = new File("\temp\*\path");

In this case, the directory is created by some other part of code which I don't have access to, which puts a timestamp in the name. So the problem would be solved if I can put * in place of the timestamp, like

 File newFile = new File("\temp\dirname-*\path");  // * is timestamp when directory was created.

Thanks for any help.

Upvotes: 0

Views: 1319

Answers (3)

Dylan
Dylan

Reputation: 13922

Creating it as you described is not possible. It is, however, possible to write an algorithm to search for a File that fits the description. In your case, you would want to create a new File("temp") then recursively search through its children (using the listFiles for any file whose isDirectory method returns true) for a file that is named "path".

Upvotes: 1

No, it is not possible to use wild cards in file names in Java.

You will need to resolve the path yourself, but it is not hard.

You might find

new java.io.File("/tmp").listFiles();

an interesting place to start.

Upvotes: 0

Jim Garrison
Jim Garrison

Reputation: 86774

If you're a programmer, you should learn that statements like "I am sure that a single directory exists at that place" will be true until they are false (and they will be false at some point).

Do the work to look in \temp\, verify that there is only one directory, then open the file with the correct path. Then when the precondition isn't true you can throw an exception or display a message.

Upvotes: 1

Related Questions