如何在 Scala 中将文件/文件从一个文件夹移动到另一个文件夹 [英] How to move file/files from one folder to another in Scala

查看:185
本文介绍了如何在 Scala 中将文件/文件从一个文件夹移动到另一个文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Scala 的新手.

I am new to Scala.

我在谷歌上搜索了很多关于如何在 Scala 中移动文件的信息,但只找到了如何在 Java 中移动文件.我尝试使用 Java import Java.io.File 移动文件,同时使用:

I've Googled a lot on how to move files in Scala, but have only found how to move files in Java. I tried to move files using Java import Java.io.File, using both:

Files.move("FileA", "FileB", StandardCopyOption.REPLACE_EXISTING);
Files.move("DirA", "DirB", StandardCopyOption.ATOMIC_MOVE); 

但是没有用.我的代码如下所示:

but it didn't work. My code looks like this:

Files.move("/public", "/public/images", StandardCopyOption.ATOMIC_MOVE);

我想将文件从 public 移动到 public/images.

I want to move files from public to public/images.

推荐答案

根据我对您问题的理解,
您正在尝试传递 String 变量而不是 java.nio.Path 变量到 Files.move().

以下方法有效:

From my understanding of your question,
you are trying to pass String variables instead of java.nio.Path variables to the Files.move().

The below way works:

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

val d1 = new File("/abcd").toPath
val d2 = new File("/efgh").toPath

Files.move(d1, d2, StandardCopyOption.ATOMIC_MOVE)

但是我在您的代码中看到了另一个问题.StandardCopyOption.REPLACE_EXISTINGStandardCopyOption.ATOMIC_MOVE 都应该可以工作,但是,您不能将父目录直接移动到它的子目录中.

However I see one more issue in your code. Both StandardCopyOption.REPLACE_EXISTING and StandardCopyOption.ATOMIC_MOVE should work but, you can't move a parent directory directly into it's child directory.

$ mv public/ public/images
mv: cannot move ‘public/’ to a subdirectory of itself, ‘public/images’

相反,您可能希望移动 public -> tmp -> public/images

Instead you might want to move public -> tmp -> public/images

这篇关于如何在 Scala 中将文件/文件从一个文件夹移动到另一个文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆