从文件路径初始化对象变量

时间:2017-09-27 19:22:10

标签: java nio java-io file-processing

servername
|-- reports
|    |-- ABC
|    |    |-- COB02May2017
|    |    |    |-- pnlreport.pdf
|    |    |    |-- balancereport.pdf
|    |    |-- COB03May2017
|    |-- CustomerB
|    |    |-- COB02May2017
|    |    |-- COB03May2017
|    |    |    |-- balancereport.pdf 
|    |-- 01CFG
|    |    |-- COB03Sep2017

我有上面的目录树来保存我的客户报告。

我有以下ReportDeliverable型号:

public class ReportDeliverable {

    private String reportId;
    private String reportName;
    private String customer;
    private String format;
    private Date cobDate;
}

ReportSchedule

public class ReportSchedule {

    private final String schedule;
    private final String reportId;
    private final String filePattern;
    private final String format;
    private final String filePath;

}

我有以下类负责提供报告可交付对象列表:

@Service
public class FileServiceImpl implements FileService {


    @Value("${reports.source-path}")
    private String sourcePath;


    @Override
    public List<ReportDeliverable> getReportDeliverables(ReportSchedule reportSchedule) {
        List<ReportDeliverable> reportDeliverables = new ArrayList<>();
        List<Path> filesToProcess = getFilesToProcess(reportSchedule);

        filesToProcess.forEach(path -> {
            //for each path returned, extract and initialise ReportDeliverable object
            ReportDeliverable reportDeliverable = new ReportDeliverable();
            //reportDeliverable.setReportName(); > pnlreport.xls
            //reportDeliverable.setFormat(); >  > PDF
            //reportDeliverable.setCobDate(); > 26-SEP-2017
            //reportDeliverable.setClient(); > CustomerA
            //reportDeliverable.setFilePath(); > \servername\reports\CustomerA\COB26Sep2017\pnlreport.pdf

            reportDeliverables.add(reportDeliverable); 
        });

        return reportDeliverables;
    }

    public List<Path> getFilesToProcess(ReportSchedule reportSchedule) {

        String pattern = reportSchedule.getFilePattern(); //e.g. pnlreport
        String format = reportSchedule.getFormat(); // PDF

        //return full paths from here based on report criteria for COB that is T-1 (day before today). ignore the rest
        // e.g. if today is 27/09/2017
        //return -> \servername\reports\CustomerA\COB26Sep2017\pnlreport.pdf, \servername\reports\CustomerB\COB26Sep2017\pnlreport.PDF

        return path;
    }
}

每天创建一个名称为 COB- {previous-day-date} 的目录,如上面的目录结构。使用Java 8,

  1. 我需要一些帮助来返回与paths中所包含的条件相关的所有文件ReportSchedules。我试图在getFilesToProcess(ReportSchedule reportSchedule)
  2. 的评论中解释一下
  3. 从路径中,我需要初始化ReportDeliverable字段,并在评论getReportDeliverables(ReportSchedule reportSchedule)
  4. 中再次解释

1 个答案:

答案 0 :(得分:1)

我对您的问题的理解是如何通过目录结构重复查找适合特定模式的文件,这些文件以包含特定格式规则后的格式化日期的方式命名。这就是我想出的。有可能进行优化,但它应该是一个起点。

如果我理解你的问题是错的,你可能会改写一下,所以我可以纠正我的答案。

import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;

public class RetrievePathsOfYesterday {

    public final static void main(String[] args) {
        String pattern = "pnlreport";
        String format = "PDF";
        String baseDir = "F:/Test";
        switch(args.length) {
        case 3:
            format = args[2];
        case 2:
            pattern = args[1];
        case 1:
            baseDir = args[0];
        }
        File root = new File(baseDir);
        File[] customerDirs = root.listFiles(file -> file.getName().toLowerCase(Locale.ENGLISH).startsWith("customer"));
        ArrayList<Path> result = new ArrayList<>();
        for (int i = 0; i < customerDirs.length; i++) {
            result.addAll(getFilesToProcess(customerDirs[i], pattern, format));
        }
        System.out.println(result);
    }

    public static List<Path> getFilesToProcess(File baseDir, String pattern, String format) {
        pattern = pattern.toLowerCase(Locale.ENGLISH);
        format = "." + format.toLowerCase(Locale.ENGLISH);
        Calendar now = Calendar.getInstance();
        now.add(Calendar.DAY_OF_YEAR, -1);
        SimpleDateFormat sdf = new SimpleDateFormat("ddMMMyyyy", Locale.ENGLISH);
        File startDir = new File(baseDir, "COB" + sdf.format(now.getTime()));
        ArrayList<Path> result = new ArrayList<>();
        getFilesToProcess(result, startDir, pattern, format);
        return result;
    }

    private static void getFilesToProcess(List<Path> resList, File baseDir, String pattern, String format) {
        System.out.println("processing " + baseDir.getAbsolutePath());
        if (!baseDir.exists()) {
            return;
        }
        File[] files = baseDir.listFiles(pathName -> {
            System.out.println("filter " + pathName.getName());
            if (pathName.isDirectory()) {
                return true;
            }
            if (!pathName.isFile()) {
                return false;
            }
            String name = pathName.getName().toLowerCase(Locale.ENGLISH);
            if (!name.startsWith(pattern)) {
                return false;
            }
            if (!name.endsWith(format)) {
                return false;
            }
            return true;
        });

        for (int i = 0; i < files.length; i++) {
            File current = files[i];
            System.out.println("Checking " + current.getAbsolutePath());
            if (current.isDirectory()) {
                getFilesToProcess(resList, current, pattern, format);
                continue;
            }
            resList.add(Paths.get(current.toURI()));
        }
    }
}

我使用以下目录结构测试了此代码:

kimmerin@harry /cygdrive/f
$ ls -R Test
Test:
CustomerA  CustomerB

Test/CustomerA:
COB26Sep2017

Test/CustomerA/COB26Sep2017:
pnlreport.pdf

Test/CustomerB:
COB26Sep2017

Test/CustomerB/COB26Sep2017:
otherreport.PDF

如果您将Test替换为servername/report,这应该与您在问题中描述的结构完全相同。这是使用默认值启动类时的输出:

  

过滤pnlreport.pdf

     

检查F:\ Test \ CustomerA \ COB26Sep2017 \ pnlreport.pdf

     

处理F:\ Test \ CustomerB \ COB26Sep2017

     

过滤otherreport.PDF

     

[F:\测试\ CustomerA \ COB26Sep2017 \ pnlreport.pdf]