【问题标题】:groovy copy files with same last modified date具有相同上次修改日期的常规复制文件
【发布时间】:2011-12-14 02:24:33
【问题描述】:

您好,我想将文件从 1 个目录复制到另一个目录,但日期必须相同。所以当from目录中的最后修改日期是14:35时,我希望它在to目录中是一样的。

如何使用 groovy 做到这一点?

【问题讨论】:

    标签: file groovy directory copy


    【解决方案1】:

    使用 AntBuilder

    new AntBuilder().copy ( file                 : 'path/to/source', 
                            tofile               : 'path/to/destination', 
                            preservelastmodified : 'true' )
    

    使用 Java/Groovy 文件 API

    def source = new File ('path/to/source')
    def destination = new File ('path/to/destination')
    
    source.withInputStream { is -> 
      destination << is 
    }
    
    destination.lastModified = source.lastModified()
    

    【讨论】:

    • 当我使用 java/groovy 文件 api 使用您的代码时,我想要复制内容更改的 jar 文件
    • 我更新了我的答案以使用InputStream 而不是Writable,它应该从源文件中进行二进制复制。请记住,&lt;&lt; 是一个文件追加操作,因此如果目标文件已经存在,这会将源内容追加到目标内容的末尾。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-02
    • 2016-10-30
    • 1970-01-01
    • 2021-10-08
    • 1970-01-01
    • 2015-02-19
    相关资源
    最近更新 更多