Umesh Kacha
Umesh Kacha

Reputation: 13666

How to get files from a directory on specific condition one file must come after another

Hi I am trying to load data into table using data files. I am using JDBC batch upload. After I load data from test.data into table, I want to validate it using expected-table.data. So in the following method first when test.data comes I want to do batch upload and then it should validate data using expected file but the following code does not work as expeted-data files comes in first iteration and test.data in second iteration. Please help I am new to file programming. Thanks in advance.

public static void loadFromFilesNValidateTable(Schema schema, final File folder)
    {

        for (final File fileEntry : folder.listFiles())
        {
            if (fileEntry.isDirectory())
            {
                loadFromFilesNValidateTable(schema,fileEntry);
            }
            else
            {
                if(fileEntry.getName().equals("test.data"))
                {
                    BatchUpload.batchUpload(schema,fileEntry.getAbsolutePath());

                }

                if(fileEntry.getName().equals("expected-table.data"))
                {
                    validateData(schema,fileEntry.getAbsolutePath());
                }
            }
        }
    } 

Upvotes: 0

Views: 120

Answers (3)

René Link
René Link

Reputation: 51353

Use a FileFilter

public static void loadFromFilesNValidateTable(TableDef schema, final File folder) {

    // Process folders recursively        
    for(final File subFolder : folder.listFiles(new DirectoryFilter())){
        loadFromFilesNValidateTable(schema, subFolder);
    }

    // Process data files
    for (final File dataFileEntry : folder.listFiles(new FileNameFilter("test.data"))) {
        BatchUpload.batchUpload(schema,dataFileEntry.getAbsolutePath());
    }

    // Process expected files
    for (final File expectedFileEntry : folder.listFiles(new FileNameFilter("expected-table.data"))) {
        validateData(schema,expectedFileEntry.getAbsolutePath());
    }
} 

public class FileNameFilter implements FileFilter {

        private String name;

        public FileNameFilter(String name){
            this.name = name;
        }

    public boolean accept(File pathname){
        return pathname.getName().equals(name)
    }
}

public class DirectoryFilter implements FileFilter {

    public boolean accept(File pathname){
        return pathname.isDirectory();
    }
}

Note: Apache commons-io provides a lot of FileFilters ready to use http://commons.apache.org/proper/commons-io/javadocs/api-2.4/org/apache/commons/io/filefilter/package-summary.html

Upvotes: 2

JB Nizet
JB Nizet

Reputation: 691685

You should change the algorithm to something like this:

for (each directory in current directory) {
    loadFromFilesNValidateTable(schema, directory);
}

if (current directory contains file "test.data") {
    batchUpload();
    if (current directory contains file "expected-table.data") {
        validateData();
    }
}

Upvotes: 0

CodePi
CodePi

Reputation: 13

I have 2 things to suggest you: 1. Change equals method with matches method. Matches method is the best method to compare Strings. 2. Try to separate the moment in which you do batchUpload from that in which you do validateData. Maybe you jump some files. For example the algorithm finds first the "expected-table.data" file and then the "test.data" file. In this case it doesn't validate the file. I hope my answer is usefull and I hope I have understood your problem : )

Upvotes: 0

Related Questions