java - 读取模型的文件路径部分

标签 java java-8

假设我有以下Domain,如果您愿意,它可以对要传递的文件进行建模:

public class Report {

    private String reportId;
    private String reportName;
    private String client;
    private String format;
    private Date cobDate;
    private String filePath;

}

public enum ReportType {

    PNL_REPORT("11", "pnlreport"),
    BALANCE_SHEET("20", "balance"),

    private final String reportId;
    private final String name;

    private ReportType(String reportId, String name) {
        this.reportId = reportId;
        this.name = name;
    }
}

鉴于我有以下Service实现:

@Service
public class FileService {

    @Autowired
    private FileRepository fileRepository;


    public List<Report> getReport(String filePattern, String format) {

        List<Report> reportDeliverables = new ArrayList<>();

        List<File> filesToSend = fileRepository.getFilesToSend(sourcePath, filePattern, format);

        \\C:\pathToReports\ClientABC\COB28Sep2017\pnlreport.pdf

        \\return list of report objects, initialised. 
    }

}

我正在使用 Java 8,想知道如何有效地将列表 filesToSend 中的每个 File 对象转换为 Report 字段,如下所示如下:

reportID -> 从查看 id 的 Enum 派生(name 与不带后缀的文件名匹配)

报告名称 -> pnlreport.pdf

客户端 -> clientABC

cobDate-> 2017 年 9 月 28 日

格式 -> pdf

文件路径 -> C:\pathToReports\ClientABC\COB28Sep2017\pnlreport.pdf

该路径始终包含源,后跟客户端、cob 日期,然后是报告文件的名称。

最佳答案

据我了解您的问题已有解决方案。请注意,ReportFile 中的正则表达式仅满足类似 Windows 的文件路径。对于其他操作系统,将需要其他正则表达式。

enum ReportType {

    PNL_REPORT("11", "pnlreport"),
    BALANCE_SHEET("20", "balance");

    private final String reportId;
    private final String name;

    ReportType(String reportId, String name) {
        this.reportId = reportId;
        this.name = name;
    }

    static ReportType fromName(String name) {
        return Arrays.stream(values())
            .filter(reportType -> reportType.name.equals(name))
            .findFirst()
            .orElseThrow(RuntimeException::new);
    }

    public String getId() {
        return reportId;
    }
}

class ReportFile {

    private static final Pattern WINDOWS_REGEX =
        Pattern.compile(".+\\\\(Client[^\\\\]+)\\\\COB([^\\\\]+)\\\\([^\\\\.]+).([^\\\\.]+)");

    private static final Pattern UNIX_REGEX =  
        Pattern.compile(".+/(Client[^/]+)/COB([^/]+)/([^/.]+).([^/.]+)");

    private final File file;

    public ReportFile(File file) {
        this.file = file;
    }

    Report toReport() {
        String filePath = file.getPath();
        Matcher matcher = WINDOWS_REGEX.matcher(filePath);

        if (!matcher.find()) {
            throw new RuntimeException("Invalid path for report: " + filePath);
        }

        String client = matcher.group(1);
        Date cobDate;
        try {
            LocalDate cobLocalDate =
                LocalDate.parse(matcher.group(2), DateTimeFormatter.ofPattern("dMMMy").withLocale(Locale.US));
            cobDate = Date.from(cobLocalDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        String reportName = matcher.group(3);
        String id = ReportType.fromName(matcher.group(3)).getId();
        String extension = matcher.group(4);

        return new Report(
            id, reportName, client, extension, cobDate, filePath
        );
    }
}

class Ex {
    public static void main(String[] args) {
        List<Report> reportDeliverables = new ArrayList<>();

        List<File> filesToSend = ImmutableList.of(
            new File("C:\\pathToReports\\ClientABC\\COB28Sep2017\\pnlreport.pdf")
        );

        System.out.println(filesToSend.stream()
            .map(ReportFile::new)
            .map(ReportFile::toReport)
            .collect(Collectors.toList()));
    }
}

关于java - 读取模型的文件路径部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46493126/

相关文章:

java - 下载 jre v8 后 Apache Tomcat 不工作

java-8 - Akka actor 和 Java8 CompletableFuture 可以安全地结合在一起吗?

java - 如果某个项目多次出现,则删除列表中该项目的所有实例

java - 在哪些情况下 Stream 操作应该是有状态的?

java - 将对象移动到列表末尾的优雅方式

java - 意外的 mongodb 更新被阻止

java - 为什么我在通过 Eclipse 使用 TomCat 运行/调试时得到 ClassNotFoundException

java - 在 Nifi 中,带有 Hive 数据库连接池服务的自定义处理器不起作用

java - 为 Anagrams 制作相同哈希码的错误方法

classpath - 无法从类路径加载 jks 文件