Sample Programs

Sample java program to get the files from a directory with specific extension


 package org.jusfortechies.example; 

 import java.io.File;
 import java.io.FilenameFilter;
 
 public class DirectoryContent {

   public static void main(String[] args) {
      
      File dir = new File("C:\\SamplePrograms");
		
      // Filter the list of files using FilenameFilter and set the filtering rule 
      // in the accept method of the FilenameFilter class 
      FilenameFilter filter = new FilenameFilter() {
            public boolean accept(File dir, String name) {
               if (name.endsWith(".txt")) {
                  return true;
               }
                  return false;
            }
      };

      // Get the filtered files
      File[] files = dir.listFiles(filter);
      for (File txtFile : files) {
         System.out.println("[Text file] " + txtFile);
      }

   }

 }




blog comments powered by Disqus