Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Groovy中复制文件_Groovy - Fatal编程技术网

如何在Groovy中复制文件

如何在Groovy中复制文件,groovy,Groovy,我需要在Groovy中复制一个文件,并看到了一些在web上实现它的方法: 一, 二, 三, 第四种方式对我来说似乎最干净,因为我不确定使用AntBuilder有多好,它有多重,我看到一些人报告Groovy版本更改的问题。 第二种方式取决于操作系统,第三种方式可能效率不高 Groovy中有没有什么东西可以像第4条语句那样复制文件,或者我应该使用Java来完成它?我正在使用AntBuilder来完成这些任务。它简单、一致、久经考验且有趣 第二种方法过于特定于操作系统(仅在您的情况下使用Linux)

我需要在Groovy中复制一个文件,并看到了一些在web上实现它的方法:

一,

二,

三,

第四种方式对我来说似乎最干净,因为我不确定使用AntBuilder有多好,它有多重,我看到一些人报告Groovy版本更改的问题。 第二种方式取决于操作系统,第三种方式可能效率不高


Groovy中有没有什么东西可以像第4条语句那样复制文件,或者我应该使用Java来完成它?

我正在使用
AntBuilder
来完成这些任务。它简单、一致、久经考验且有趣

第二种方法过于特定于操作系统(仅在您的情况下使用Linux)

第三,它的级别太低,占用了更多的资源。如果您需要在途中转换文件,这将非常有用:例如更改编码

第四个看起来太复杂了。。。NIO包在JDK中相对较新


最后,我会选择第一种选择。在那里,您可以从
copy
切换到
scp
任务,而无需几乎从头开始重新开发脚本

如果您在代码中执行此操作,只需使用以下命令:

new File('copy.bin').bytes = new File('orig.bin').bytes
如果这是与构建相关的代码,那么也可以使用Ant builder


注意,如果您确定这些文件是文本文件,那么您可以使用
.text
而不是
.bytes

如果您有Java 7,我肯定会使用

Path source = ...
Path target = ...
Files.copy(source, target)
使用该类,它可以使用符号链接和硬链接。发件人:

此类仅由在上操作的静态方法组成 文件、目录或其他类型的文件。在大多数情况下 此处定义的方法将委托给关联的文件系统 提供程序执行文件操作

作为参考:


我的第二个选项是
ant
任务和
AntBuilder

如果它是一个文本文件,我会选择:

  def src = new File('src.txt')
  def dst = new File('dst.txt')
  dst << src.text
def src=新文件('src.txt')
def dst=新文件('dst.txt')

dst这是使用独立于平台的groovy脚本的方法。如果有人有问题,请在评论中提问

def file = new File("java/jcifs-1.3.18.jar")
this.class.classLoader.rootLoader.addURL(file.toURI().toURL())

def auth_server = Class.forName("jcifs.smb.NtlmPasswordAuthentication").newInstance("domain", "username", "password")
def auth_local = Class.forName("jcifs.smb.NtlmPasswordAuthentication").newInstance(null, "local_username", "local_password")

def source_url = args[0]
def dest_url = args[1]
def auth = auth_server

//prepare source file
if(!source_url.startsWith("\\\\"))
{
  source_url = "\\\\localhost\\"+ source_url.substring(0, 1) + "\$" + source_url.substring(1, source_url.length());
  auth = auth_local  
}
source_url = "smb:"+source_url.replace("\\","/");
println("Copying from Source -> " + source_url);
println("Connecting to Source..");

def source = Class.forName("jcifs.smb.SmbFile").newInstance(source_url,auth)
println(source.canRead());


// Reset the authentication to default
auth = auth_server

//prepare destination file
if(!dest_url.startsWith("\\\\"))
{
  dest_url = "\\\\localhost\\"+ dest_url.substring(0, 1) + "\$" +dest_url.substring(2, dest_url.length());
  auth = auth_local  
}

def dest = null
dest_url = "smb:"+dest_url.replace("\\","/");
println("Copying To Destination-> " + dest_url);
println("Connecting to Destination..");

dest = Class.forName("jcifs.smb.SmbFile").newInstance(dest_url,auth)
println(dest.canWrite());

if (dest.exists()){
  println("Destination folder already exists");
}
source.copyTo(dest);

要附加到现有文件,请执行以下操作:

def src = new File('src.txt')
def dest = new File('dest.txt')
dest << src.text
我喜欢这样:

def file = new File("old.file")
def newFile = new File("new.file")
Files.copy(file.toPath(), newFile.toPath())

用于在Jenkins Groovy中复制文件

对于Linux:
试试看{
回显“将文件复制到所需位置”
sh'''cd/安装/选择/
cp/install/opt/ssl.ks/var/local/system/“”
echo“文件复制成功”
}
捕获(例外e){
“复制文件失败”错误
}
**对于Windows:**
试一试{
回显“将文件复制到所需位置”
蝙蝠“”@回声关闭
复制C:\\Program Files\\install\\opt\\ssl.ks C:\\ProgramData\\install\\opt''
echo“文件复制成功”
}
捕获(例外e){
“复制文件失败”错误

}
虽然这很好而且简单,但它会将整个文件加载到内存中,而对于大文件,这将失败。还有一些方法可以流式处理它<代码>新文件()。使用stream{}
-我选择了最简单的方法。你是说NIO2(在Java7中引入)。NIO(新I/O)是在Java1.4中引入的。这个问题本身就是“如何在groovy中复制文件”的好答案:)在Gradle中使用它时,
无法解析Java.NIO.files.path类。我使用
import java.nio.*
import java.nio.file.*
。@MatthiasBraun是对的,它是。这是java而不是Groovy。所以这不是答案。有关groovy答案,请参见此链接。由于既位于JVM之上,又由于groovy在某种程度上是Java的超集(GDK等),因此“Java不是groovy”毫无意义。该链接基本上显示了这个问题的其他答案,并引用它:“注意,这可能会很慢,并且内存不足,因为这段代码在复制之前会将整个文件加载到内存中”。这是一个相当大的问题,因此我将再次指出,尽管还有其他groovier解决方案,但最好委托给底层文件系统。无论如何,感谢您对您的否决票发表评论。您如何从
字符串创建
路径
?这与公认的答案有何不同?甚至有接受路径和文件的方法吗?File类有这个方法是有原因的。这种方法就是使用这种方法(被接受的答案没有使用这种方法)。在Groovy中,当需要导入路径时,文件已经包含在内。如果您主要使用路径,那么接受的答案更好,但是如果您使用文件这种方式更方便,但是您可能会认为不是这样。
  def src = new File('src.txt')
  def dst = new File('dst.txt')
  dst << src.text
def file = new File("java/jcifs-1.3.18.jar")
this.class.classLoader.rootLoader.addURL(file.toURI().toURL())

def auth_server = Class.forName("jcifs.smb.NtlmPasswordAuthentication").newInstance("domain", "username", "password")
def auth_local = Class.forName("jcifs.smb.NtlmPasswordAuthentication").newInstance(null, "local_username", "local_password")

def source_url = args[0]
def dest_url = args[1]
def auth = auth_server

//prepare source file
if(!source_url.startsWith("\\\\"))
{
  source_url = "\\\\localhost\\"+ source_url.substring(0, 1) + "\$" + source_url.substring(1, source_url.length());
  auth = auth_local  
}
source_url = "smb:"+source_url.replace("\\","/");
println("Copying from Source -> " + source_url);
println("Connecting to Source..");

def source = Class.forName("jcifs.smb.SmbFile").newInstance(source_url,auth)
println(source.canRead());


// Reset the authentication to default
auth = auth_server

//prepare destination file
if(!dest_url.startsWith("\\\\"))
{
  dest_url = "\\\\localhost\\"+ dest_url.substring(0, 1) + "\$" +dest_url.substring(2, dest_url.length());
  auth = auth_local  
}

def dest = null
dest_url = "smb:"+dest_url.replace("\\","/");
println("Copying To Destination-> " + dest_url);
println("Connecting to Destination..");

dest = Class.forName("jcifs.smb.SmbFile").newInstance(dest_url,auth)
println(dest.canWrite());

if (dest.exists()){
  println("Destination folder already exists");
}
source.copyTo(dest);
def src = new File('src.txt')
def dest = new File('dest.txt')
dest << src.text
def src = new File('src.txt')
def dest = new File('dest.txt')
dest.write(src.text)
def file = new File("old.file")
def newFile = new File("new.file")
Files.copy(file.toPath(), newFile.toPath())