我正在尝试使用以下代码将多个文件从一个文件夹复制到另一个文件夹
val pathCorpus = Files.copy(
Paths.get("src/main/resources/corpusDirectory/corpus.mallet"),
Paths.get("src/main/resources/corpus.mallet"),
StandardCopyOption.REPLACE_EXISTING
)
val pathInferencer = Files.copy(
Paths.get("src/main/resources/corpusDirectory/inferencer"),
Paths.get("src/main/resources/inferencer"),
StandardCopyOption.REPLACE_EXISTING
)
因为我需要复制两个文件,所以我使用了两次Files.copy
。代码正在工作,但我相信应该有更好的方法来编写代码,比如递归复制文件。
发布于 2019-05-30 23:57:40
您可以使用FileUtils.copyDirectory()
val source = "C:/your/source";
val srcDir = new File(source);
val destination = "C:/your/destination";
val destDir = new File(destination);
FileUtils.copyDirectory(srcDir, destDir);
谢谢,
发布于 2019-05-31 00:00:42
我喜欢用bash
import scala.sys.process.stringToProcess
val copyFiles: String = s"cp -R src/main/resources/corpusDirectory/ src/main/resources/".!!
您可以使用bash cp:https://www.cyberciti.biz/faq/copy-command/
发布于 2019-05-31 02:39:19
better-files
支持通过source.copyTo(destination)
语法递归复制目录。下面是一个有效的示例
import better.files._
object Hello extends App {
val source = file"/your/sourceDir"
val destination = file"/your/destinationDir"
source.copyTo(destination)
}
其中我们需要以下依赖项
libraryDependencies += "com.github.pathikrit" %% "better-files" % "3.8.0"
https://stackoverflow.com/questions/56381165
复制相似问题