Tuesday, 4 August 2015

How to read all files in a folder in Java.

import java.io.File;

public class Readallfiles {

     //File location
     static File mainFolder = new File("D:\\Images");

     public static void main(String[] args)
     {
         Readallfiles lf = new Readallfiles();
         lf.getFiles(lf.mainFolder);

     }
     public void getFiles(File f){
         File files[];
         if(f.isFile())
     // File name
             System.out.println(f.getName());
         else{
             files = f.listFiles();
             for (int i = 0; i < files.length; i++) {
                 getFiles(files[i]);
             }
         }
     }
}

No comments:

Post a Comment