gpt4 book ai didi

grails - 获取文件列表

转载 作者:行者123 更新时间:2023-12-02 14:28:45 24 4
gpt4 key购买 nike

我有一个名为“import”的目录,想获取所有文件及其对应的日期(基于文件名)。目录的样本内容是这样的:

  • 输入_02202010.xls
  • input_02212010.xls
  • input_02222010.xls

  • 我想要一个包含文件路径和Date变量的Map。

    谁能告诉我Groovy将如何解决这个问题?

    最佳答案

    就像在Java中一样,使用new File("/foo/bar/import").list()获取文件名。然后从字符串创建文件对象,并检查lastModified()的最后修改日期。

    编辑:
    Groovy在java.io.File中添加了eachFile()方法,我们可以使用它来使它更具常规性。

    要从文件名中提取日期,请使用

    Date d = new java.text.SimpleDateFormat("MMddyyyy").parse(filename.substring(6,14))

    要将其全部放入 map 中(使用文件名作为键,使用日期作为值,尽管是多余的):
    def df = new java.text.SimpleDateFormat("MMddyyyy")
    def results = [:]
    new File("/foo/bar/import").eachFile() { file ->
    results.put(file.getName(), df.parse(file.getName().substring(6,14)))
    }

    results

    关于grails - 获取文件列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2324683/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com