加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

如何复制Groovy中的文件

发布时间:2020-12-14 16:38:28 所属栏目:大数据 来源:网络整理
导读:我需要在Groovy中复制一个文件,并且看到了一些在网络上实现它的方法: 1 new AntBuilder().copy( file:"$sourceFile.canonicalPath",tofile:"$destFile.canonicalPath") 2 command = ["sh","-c","cp src/*.txt dst/"]Runtime.getRuntime().exec((String[])
我需要在Groovy中复制一个文件,并且看到了一些在网络上实现它的方法:

1

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

2

command = ["sh","-c","cp src/*.txt dst/"]
Runtime.getRuntime().exec((String[]) command.toArray())

3

destination.withDataOutputStream { os->  
    source.withDataInputStream { is->  
       os << is  
    }  
 }

4

import java.nio.file.Files
import java.nio.file.Paths
Files.copy(Paths.get(a),Paths.get(b))

第四种方式对我来说似乎最干净,因为我不知道使用AntBuilder有多好,多么沉重,我看到有人报告Groovy版本的变化。
第二种方式是依赖于操作系统,第三种方式可能不高效。

在Groovy中有什么可以复制像第四个语句中的文件,还是应该使用Java呢?

解决方法

如果你有Java 7,我一定会去的

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

使用java.nio.file.Path课程,它可以与符号和硬链接工作。从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
.

作为参考:

Copy files from one folder to another with Groovy

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

我的第二个选择将是AntBuilder的蚂蚁任务。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读