Search directory for any .XML file

2019-07-31 22:58发布

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.

标签: java xml java-io
5条回答
够拽才男人
2楼-- · 2019-07-31 23:18

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:

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 */
   }
}
查看更多
smile是对你的礼貌
3楼-- · 2019-07-31 23:19

You need to use a 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 are wondering what a Glob is: here it is explained.

查看更多
Luminary・发光体
4楼-- · 2019-07-31 23:31

You can try using this code for your 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 it helps..

查看更多
神经病院院长
5楼-- · 2019-07-31 23:35

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 of your xml files are in the File[] xmlFiles.

查看更多
祖国的老花朵
6楼-- · 2019-07-31 23:36

Alternative 1:

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

Alternative 2:

You can also use listFiles(FilenameFilter filter)

查看更多
登录 后发表回答