简体   繁体   中英

Filter files using a wildcard character (java)

Using a wildcard character I want to process files in a directory. If a wildcard character is specified I want to process those files which match the wildcard char else if not specified I'll process all the files. Here's my code

   List<File> fileList;
   File folder = new File("Directory");
   File[] listOfFiles = folder.listFiles();
    if(prop.WILD_CARD!=null) {  
        Pattern wildCardPattern = Pattern.compile(".*"+prop.WILD_CARD+"(.*)?.csv",Pattern.CASE_INSENSITIVE);
        for(File file: listOfFiles) {
            Matcher match = wildCardPattern.matcher(file.getName());
            while(match.find()){
                String fileMatch = match.group();
                if(file.getName().equals(fileMatch))  {
                    fileList.add(file); // doesn't work
                }
            }
        }
    }
    else
        fileList = new LinkedList<File>( Arrays.asList(folder.listFiles()));

I'm not able to put the files that match wildcard char in a separate file list. Pls help me to modify my code so that I can put all the files that match wildcard char in a separate file list. Here I concatenate prop.WILD_CARD in my regex, it can be any string, for instance if wild card is test, my pattern is . test(. )?.csv. And I want to store the files matching this wildcard and store it in a file list.

I just tested this code and it runs pretty well. You should check for logical errors somewhere else.

    public static void main(String[] args) {

    String WILD_CARD = "";
     List<File> fileList = new LinkedList<File>();
       File folder = new File("d:\\");
       File[] listOfFiles = folder.listFiles();
        if(WILD_CARD!=null) {  
            Pattern wildCardPattern = Pattern.compile(".*"+WILD_CARD+"(.*)?.mpp",Pattern.CASE_INSENSITIVE);
            for(File file: listOfFiles) {
                Matcher match = wildCardPattern.matcher(file.getName());
                while(match.find()){
                    String fileMatch = match.group();
                    if(file.getName().equals(fileMatch))  {
                        fileList.add(file); // doesn't work
                    }
                }
            }
        }
        else
            fileList = new LinkedList<File>( Arrays.asList(folder.listFiles()));

        for (File f: fileList) System.out.println(f.getName());
}

This returns a list of all *.mpp files on my D: drive.

I'd also suggest using

        for (File file : listOfFiles) {
            Matcher match = wildCardPattern.matcher(file.getName());
            if (match.matches()) {
                fileList.add(file);
            }
        }

I would suggest you look into the FilenameFilter class and see if it helps simplify your code. As for your regex expression, I think you need to escape the "." character for it to work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM