Java 从文件路径初始化对象变量
我有上面的目录树来保存我的客户报告 我有以下可提交报告的模型: 报告时间表: 我有以下类别负责提供报告可交付对象列表:Java 从文件路径初始化对象变量,java,nio,java-io,file-processing,Java,Nio,Java Io,File Processing,我有上面的目录树来保存我的客户报告 我有以下可提交报告的模型: 报告时间表: 我有以下类别负责提供报告可交付对象列表: public class ReportSchedule { private final String schedule; private final String reportId; private final String filePattern; private final String format; private final
public class ReportSchedule {
private final String schedule;
private final String reportId;
private final String filePattern;
private final String format;
private final String filePath;
}
每天创建一个名为COB-{previor day date}的目录,如上述目录结构所示。使用Java8
我需要一些帮助来返回所有与ReportSchedules中保存的条件有关的文件路径。我试图在getFilesToProcessReportSchedule中的注释中进行解释
从路径中,我需要初始化ReportDeliverable字段,这在注释GetReportDeliverableReportSchedule中再次解释
我对您的问题的理解是,如何反复遍历目录结构,在目录中找到符合特定模式的文件,这些文件的命名方式包括遵循特定格式规则的格式化日期。这是我想到的。有可能进行优化,但这应该是一个起点 如果我理解错了你的问题,你可以重新措辞,这样我就可以更正我的答案
@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;
}
}
如果用servername/report替换Test,则应该与问题中描述的结构完全相同。这是使用默认值启动类时的输出:
过滤器pnlreport.pdf
正在检查F:\Test\CustomerA\COB26Sep2017\pnlreport.pdf
正在处理F:\Test\CustomerB\COB26Sep2017
过滤其他报告.PDF
[F:\Test\CustomerA\COB26Sep2017\pnlreport.pdf]
从快速浏览答案中可以看出,客户是独一无二的,并且不会遵循其相应目录的模式。@user2781389我将用于测试的目录结构添加到了答案中。谢谢。由于客户目录级别的名称(如客户)中没有模式,您可以更新您的答案以读取所有客户目录。我们是否可以使用文件流。查找并提供maxdepth来读取所有customerdir?也许在这个答案或重构中提供一些优化范围?如何将COB date作为日期对象读取?
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;
}
}
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