Wednesday 1 May 2013


Get list of files in a directory using Java

While doing an automation I came up with a problem where I need to get a list of all files in a Directory.
So just though to share the same with you the program to do so. I had made used of recursion in the following programs to get the list of files. Following are the two programs one will give you a ArrayList of files with their absolute path where as another will give you an ArrayList of File object of respective files.
Following program will store the absolute path of all the files inside a directory to the fileList argument provided.
?
1
2
3
4
5
6
7
8
9
10
11
public void listDirectory(File f, ArrayList< String> fileList) {
        File[] listOfFiles = f.listFiles();
        for (int i = 0; i < listOfFiles.length; i++) {
            if (listOfFiles[i].isFile()) {
                fileList.add(listOfFiles[i].getAbsolutePath());
            } else if (listOfFiles[i].isDirectory()) {
                listDirectory(listOfFiles[i], fileList);
            }
        }
    }

Following  program will store the File object of the files that are found into the respective fileList Array provided as an argument to the function
?
1
2
3
4
5
6
7
8
9
10
11
public void listDirectory(File f, ArrayList< File> fileList) {
        File[] listOfFiles = f.listFiles();
        for (int i = 0; i < listOfFiles.length; i++) {
            if (listOfFiles[i].isFile() ) {
                fileList.add(listOfFiles[i]);
            } else if (listOfFiles[i].isDirectory() ) {
                listDirectory(listOfFiles[i], fileList);
            }
        }
    }

Angular JS Protractor Installation process - Tutorial Part 1

                     Protractor, formally known as E2E testing framework, is an open source functional automation framework designed spe...