How to copy files in Groovy

后端 未结 8 1682
余生分开走
余生分开走 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

    If you have Java 7, I would definitely go with

    Path source = ...
    Path target = ...
    Files.copy(source, target)
    

    With the java.nio.file.Path class, it can work with symbolic and hard links. From java.nio.file.Files:

    This class consists exclusively of static methods that operate on files, directories, or other types of files. In most cases, the methods defined here will delegate to the associated file system provider to perform the file operations.

    Just as references:

    Copy files from one folder to another with Groovy

    http://groovyconsole.appspot.com/view.groovy?id=8001

    My second option would be the ant task with AntBuilder.

提交回复
热议问题