+92 332 4229 857 99ProjectIdeas@Gmail.com

How to show .txt extension files in a folder (Java)


How to show (.txt) extension files in a folder:
The following code shows you how to show the .txt files in folder. For this you have to implement an FilenameFilter interface which let you do this.
 
Code

public class Test implements FilenameFilter {
      
   private String ext;
      
   public Test(String _ext) {
           
          this.ext = "." + _ext;
   }
       
  // overriding method of interface FileameFilter
   public boolean accept(File directoryName, String fileName) {
             
       return fileName.endsWith(ext);
   }
       
public static void main(String[] args) {

 // Showing Those Files Which Have .txt Extension
      
     FilenameFilter onlyExt = new Test("txt");

     String[] fileListTxt = fl.list(onlyExt);
             
     System.out.println("Directory Contains Files With .txt Extension ");
             
         for (int i = 0 ; i < fileListTxt.length ; i++) {
                  
                System.out.println(fileListTxt[i]);
         }
    }
}
      

0 comments: