简体   繁体   English

如何从特定条件下的目录中获取文件,一个文件必须接着另一个文件

[英]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. 我正在使用JDBC批量上传。 After I load data from test.data into table, I want to validate it using expected-table.data. 将test.data中的数据加载到表中后,我想使用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. 所以在下面的方法中,首先当test.data出现时,我想进行批量上传,然后它应该使用预期文件验证数据,但是下面的代码不能用作第一次迭代中的详细数据文件和第二次迭代中的test.data。 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());
                }
            }
        }
    } 

Use a FileFilter 使用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 注意: Apache commons-io提供了很多可以使用的FileFilter http://commons.apache.org/proper/commons-io/javadocs/api-2.4/org/apache/commons/io/filefilter/package-summary。 HTML

I have 2 things to suggest you: 1. Change equals method with matches method. 我有两件事要建议你:1。用匹配方法改变equals方法。 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. 2.尝试将您执行batchUpload的时刻与执行validateData的时刻分开。 Maybe you jump some files. 也许你跳了一些文件。 For example the algorithm finds first the "expected-table.data" file and then the "test.data" file. 例如,算法首先找到“expected-table.data”文件,然后找到“test.data”文件。 In this case it doesn't validate the file. 在这种情况下,它不验证文件。 I hope my answer is usefull and I hope I have understood your problem : ) 我希望我的答案很有用,我希望我能理解你的问题:)

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();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM