Search directory for any .XML file

I am trying to get some of the code to work. The goal is to check if the .XML file is in a specific directory.

This is what I still have.

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 a file ending in .XML, am I missing something simple here? Is there an easier way to check if at least one .XML file exists in the specified directory?

early.

+1
java xml java-io
Apr 22 '13 at 9:20
source share
5 answers

You can use this:

  File dir = new File("saves"); if (dir.isDirectory()) { File[] xmlFiles = dir.listFiles(new FilenameFilter() { @Override public boolean accept(File folder, String name) { return name.toLowerCase().endsWith(".xml"); } }); } 

Now all your xml files are in File[] xmlFiles .

+8
Apr 22 '13 at 9:27
source share

Alternative 1:

You can use PathMatcher to search for files using a specific pattern.

Alternative 2:

You can also use listFiles (FilenameFilter filter)

+3
Apr 22 '13 at 9:24 on
source share

You need to use PathMatcher, something like:

 PathMatcher matcher = FileSystems.getDefault().getPathMatcher("glob:*.{xml}"); Path filename = ...; if (matcher.matches(filename)) { System.out.println(filename); } 

From the Oracle documentation here . And if you're interested in what Glob is: here .

+1
Apr 22 '13 at 9:27
source share

Separate the filter part from the search path and list the files in the search path with the file name filter, filtering only xml files. If the list size is greater than 0, you know that the search path contains at least one XML file. See the sample code below:

 File f = new File("C:\\"); if (f.isDirectory()){ FilenameFilter filter = new FilenameFilter() { @Override public boolean accept(File dir, String name) { if(name.endsWith(".xml")){ return true; } return false; } }; if (f.list(filter).length > 0){ /* Do Something */ } } 
+1
Apr 22 '13 at 9:41
source share

You can try using this code for reference ...

 import java.io.*; public class FindCertainExtension { private static final String FILE_DIR = "FOLDER_PATH"; private static final String FILE_TEXT_EXT = ".xml"; public static void main(String args[]) { new FindCertainExtension().listFile(FILE_DIR, FILE_TEXT_EXT); } public void listFile(String folder, String ext) { GenericExtFilter filter = new GenericExtFilter(ext); File dir = new File(folder); if(dir.isDirectory()==false){ System.out.println("Directory does not exists : " + FILE_DIR); return; } // list out all the file name and filter by the extension String[] list = dir.list(filter); if (list.length == 0) { System.out.println("no files end with : " + ext); return; } for (String file : list) { String temp = new StringBuffer(FILE_DIR).append(File.separator) .append(file).toString(); System.out.println("file : " + temp); } } // inner class, generic extension filter public class GenericExtFilter implements FilenameFilter { private String ext; public GenericExtFilter(String ext) { this.ext = ext; } public boolean accept(File dir, String name) { return (name.endsWith(ext)); } } } 

Hope this helps.

0
Apr 22 '13 at 9:29
source share



All Articles