移动正在处理的文件

Moving a file in processing

我正在尝试移动正在处理的文件。

import java.util.Base64;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;


String source = "C:\test.jpeg";
String newdir = "C:\test123.jpeg";

void setup() {

Files.move(source, newdir.resolve(source.getFileName()));


}

我查看了 this 并试图让它工作,但是我收到一个错误消息,指出函数 getFileName() 不存在。我也找过这个,但没有找到太多。有人可以指出将文件从一个目录移动到另一个目录的正确方向吗?

看看这个:

import java.nio.file.*;

String source = "C:\test\1.jpeg";
String newdir = "C:\test123\1.jpeg";

void setup() {
    try {
        Path temp = Files.move(Paths.get(source), Paths.get(newdir));
    } catch (IOException e) {
        print(e);
    }
}

几点 - 在指定路径时使用 \ 而不是单个 \。其次, getFileName() 只能应用于 Path 对象,而不能应用于 String,这导致了您在问题中的错误。顺便说一下,与 resolve(String s) 方法一样,它只能应用于 Path,不能应用于 String。

使用路径:

import java.nio.file.*;

Path source = Paths.get("...");
Path newdir = Paths.get("...");

void setup() {
    try {
        Files.move(source, newdir);
    } catch (IOException e) {
        print(e);
    }
}