-2

am trying to compare all the values in my arraylist with all the files in my c:drive

but the code below does not work

    List<String> list = new ArrayList<String>();
    list.add("*.txt,*.docx");
    File file = new File("c:\\*.txt");
    if (file.equals(list)) {
    System.out.println("file was found");
    }else{System.out.println("nothing was found");
    }

so the idea is that anytime i run my the code my arraylist would compare itself with my c: drive and list all files that has the extension of "docx and txt" out.

i realised that when i use wildcards it didn't work.

Ribo01
  • 59
  • 1
  • 7
  • You will need to get a list of all the filenames http://stackoverflow.com/questions/5694385/getting-the-filenames-of-all-files-in-a-folder – explv Jul 07 '16 at 15:34

1 Answers1

0

What you need is a FileNameFilter to get all files that pertain to your requirements

Here is an example of getting all *.txt files from current directory. You can implement FileNameFilter to create your own filter that will work on your List.

    File f = new File("."); // current directory

    FilenameFilter textFilter = new FilenameFilter() {
        public boolean accept(File dir, String name) {
            String lowercaseName = name.toLowerCase();
            if (lowercaseName.endsWith(".txt")) {
                return true;
            } else {
                return false;
            }
        }
    };

    File[] files = f.listFiles(textFilter);

Hope this helps.

Sanjeev
  • 9,876
  • 2
  • 22
  • 33