I am trying to get a piece of code working. The aim is to check if there is an .XML file in a certain directory.
This is what I've got so far.
File f = new File("saves/*.xml");
if(f.exists()) {
/* Do Something */
} else {
/* do something else */
}
I'm trying to use a wildcard to search for any file ending in .XML, am I missing something simple here? Is there an easier way to check that at least one .XML file exists in a specified directory?
thanks in advance.
Separate the filter part from the search path and list the files in the search path with a file name filter, filtering only the xml files. If the list sizee is greater than 0 then you know that the search path contains atleast one xml file. See sample code below:
You need to use a PathMatcher, something like:
From the Oracle documentation here. And if you are wondering what a Glob is: here it is explained.
You can try using this code for your reference...
Hope it helps..
You can use this:
Now all of your xml files are in the
File[] xmlFiles
.Alternative 1:
You can use PathMatcher to search for files using specific pattern.
Alternative 2:
You can also use listFiles(FilenameFilter filter)