Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/16.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
Java 如何在Scala中将文件从一个文件夹移动到另一个文件夹_Java_Scala - Fatal编程技术网

Java 如何在Scala中将文件从一个文件夹移动到另一个文件夹

Java 如何在Scala中将文件从一个文件夹移动到另一个文件夹,java,scala,Java,Scala,我是斯卡拉的新手 我在谷歌上搜索了很多,但只找到了如何在Java中移动文件。我尝试使用Java移动文件: 导入Java.io.File 而两者: Files.move(“FileA”,“FileB”,StandardCopyOption.REPLACE_EXISTING)和文件.move(“DirA”,“DirB”,StandardCopyOption.ATOMIC_move) 如何在Scala中将文件从一个文件夹移动到另一个文件夹?您可以在Scala中使用Java API将文件从一个位置移动到

我是斯卡拉的新手

我在谷歌上搜索了很多,但只找到了如何在Java中移动文件。我尝试使用Java移动文件:

导入Java.io.File

而两者:
Files.move(“FileA”,“FileB”,StandardCopyOption.REPLACE_EXISTING)
文件.move(“DirA”,“DirB”,StandardCopyOption.ATOMIC_move)


如何在Scala中将文件从一个文件夹移动到另一个文件夹?

您可以在Scala中使用Java API将文件从一个位置移动到另一个位置。下面是执行相同操作的代码

import java.io.File
import java.nio.file.{Files, StandardCopyOption}

val source = new File("path-to-source-directory").toPath
val destination = new File("path-to-destination-directory").toPath
Files.move(source, destination, StandardCopyOption.ATOMIC_MOVE)

如果你必须做大量的文件处理,我建议你

这很容易

import better.files._
File("/path/one/file").moveToDirectory(File("path/two"))

一种简单的方法是在scala中使用sys proecess build。 就像使用bash一样

如果要将“1/1.txt”移动到“2”

$tree
.
├── 1.
│   └── 1.txt
└── 2.
在scala REPL中:

scala>导入scala.sys.process_
导入scala.sys.process_
scala>“树”。!!。mkString
res0:String=
".
├── 1.
│   └── 1.txt
└── 2.
2个目录,1个文件
"
scala>“mv 1/1.txt 2”。!!
res1:String=“”
scala>“树”。!!。mkString
res2:字符串=
".
├── 1.
└── 2.
└── 1.txt
2个目录,1个文件
将文件移动到另一个目录有点不同

scala>“树”。!!.mkString
res3:字符串=
".
├── 1.
└── 2.
├── 1.txt
└── 2.txt
2个目录,2个文件
"
scala>序列(“/bin/sh”、“-c”、“mv 2/*1”)。!!
res6:String=“”
scala>“树”。!!.mkString
res7:字符串=
".
├── 1.
│   ├── 1.txt
│   └── 2.txt
└── 2.
2个目录,2个文件
"
另见此处: