使用Java将文件从一个目录移动到另一个目录
我们可以使用Files.move()API将文件从一个目录移动到另一个目录。以下是move方法的语法。
public static Path move(Path source,Path target,CopyOption... options) throws IOException
哪里
source-要移动的文件的源路径
目标-文件的目标路径移动
options-选项,例如REPLACE_EXISTING,ATOMIC_MOVE
示例
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class Tester {
public static void main(String[] args) {
//将文件从D:/temp/test.txt移至D:/temp1/test.txt-
//确保temp1文件夹存在
moveFile("D:/temp/test.txt", "D:/temp1/test.txt");
}
private static void moveFile(String src, String dest ) {
Path result = null;
try {
result = Files.move(Paths.get(src), Paths.get(dest));
} catch (IOException e) {
System.out.println("Exception while moving file: " + e.getMessage());
}
if(result != null) {
System.out.println("文件已成功移动。");
}else{
System.out.println("文件移动失败。");
}
}
}输出结果
文件已成功移动。
热门推荐
1 带狗狗训练
4 听力训练狗狗
6 怎训练狗狗
7 腹肌训练狗狗
8 网球训练狗狗
9 记叙文训练狗狗
10 狗狗太凶怎么训练
11 核心力量训练狗狗
12 狗狗乱叫怎么训练
13 发音训练狗狗
14 怎么训练狗狗叼东西给主人
15 荆州训练狗狗
16 篮球训练狗狗
17 做训练狗狗
18 训练狗狗坐姿