Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I have to filter files from Directory which matches the same file extension from ArrayList which I added

        String folderpath = "G:\\AE_IntegrationComp";
        //Above is Folder where is Different files are present
        
        List <String>filet = new ArrayList<String>();
        filet.add(".txt");
        filet.add(".doc");
        //extension which I added
        
        for(String str : filet)
        {
            
        }
        File directory = new File(folderpath);
        for(File list : directory.listFiles())
        {
            if(list.getName().contains(""))
            {
                System.out.println(list.getName());
            }
        }

I have to check if Directory is empty or not
if not,
file extension in Arraylist should matched with extensions Are available in Directory
and print files that matched

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

What you wanna do is:

  1. Iterate through all files into a directory (which you have done)
  2. Check if it has a certain extension (it ends with a certain string)
  3. Print the file name if that matches.
public class Main {

  // Here we have a constant containing the interesting file extensions
  public static final String[] extensions = new[] { ".txt", ".doc" };

  // This helper function will tell us whether 
  // a file has one of the interesting file extensions
  public static boolean matchesExtension(File file) {
    for(String ext : extensions) {
      if(file.getName().endsWith(ext)) { 
         return true; 
      }
    }
    return false;
  }


  public static void main(String[] args) {
        String folderpath = "G:\\AE_IntegrationComp";
        File directory = new File(folderpath);
        // Iterate through all files in the directory
        for(File file : directory.listFiles()){
            if(matchesExtension(file)){
                System.out.println(file.getName());
            }
        }

  }
}

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading