用java代码实现将指定的图片文件从一个路径移动到另一个路径
时间: 2023-05-17 09:06:41 浏览: 235
可以使用以下代码实现:
```java
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class MoveFileExample {
public static void main(String[] args) {
String sourcePath = "C:/source/image.jpg";
String destinationPath = "C:/destination/image.jpg";
Path source = Paths.get(sourcePath);
Path destination = Paths.get(destinationPath);
try {
Files.move(source, destination);
System.out.println("File moved successfully.");
} catch (IOException e) {
System.out.println("Failed to move file: " + e.getMessage());
}
}
}
```
其中,sourcePath 和 destinationPath 分别为源文件路径和目标文件路径。使用 Files.move() 方法将文件从源路径移动到目标路径。如果移动成功,输出 "File moved successfully.",否则输出 "Failed to move file: " + 异常信息。