How to copy files in Groovy

后端 未结 8 1677
余生分开走
余生分开走 2020-12-13 17:37

I need to copy a file in Groovy and saw some ways to achieve it on the web:

1

new AntBuilder().copy( file:\"$sourceFile.canonicalPath\", 
                  


        
相关标签:
8条回答
  • 2020-12-13 18:06

    I prefer this way:

    def file = new File("old.file")
    def newFile = new File("new.file")
    Files.copy(file.toPath(), newFile.toPath())
    
    0 讨论(0)
  • To append to existing file :

    def src = new File('src.txt')
    def dest = new File('dest.txt')
    dest << src.text
    

    To overwrite if file exists :

    def src = new File('src.txt')
    def dest = new File('dest.txt')
    dest.write(src.text)
    
    0 讨论(0)
提交回复
热议问题