java编程实现:将图片“D:\\test.png”复制到”E:\\pics\\test.png”
时间: 2023-06-05 13:06:38 浏览: 65
好的,这个问题可以回答。您可以使用Java中的FileUtils类来实现文件复制操作,具体代码如下:
```java
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.IOException;
public class FileCopyExample {
public static void main(String[] args) {
String sourceFilePath = "D:\\test.png";
String targetFilePath = "E:\\pics\\test.png";
File sourceFile = new File(sourceFilePath);
File targetFile = new File(targetFilePath);
try {
FileUtils.copyFile(sourceFile, targetFile);
System.out.println("文件复制成功!");
} catch (IOException e) {
System.err.println("文件复制失败:" + e.getMessage());
}
}
}
```
相关问题
用java编程实现:将图片“D:\\test.png”复制到”E:\\pics\\test.png”
好的,我可以回答这个问题。你可以参考下面的Java代码:
import java.io.*;
public class CopyImage {
public static void main(String[] args) {
// 源文件路径
String srcPath = "D:\\test.png";
// 目标文件路径
String destPath = "E:\\pics\\test.png";
try {
// 创建源文件和目标文件的File对象
File srcFile = new File(srcPath);
File destFile = new File(destPath);
// 创建输入流和输出流
FileInputStream fis = new FileInputStream(srcFile);
FileOutputStream fos = new FileOutputStream(destFile);
// 读取源文件并写入目标文件
byte[] buffer = new byte[1024];
int len = 0;
while ((len = fis.read(buffer)) != -1) {
fos.write(buffer, 0, len);
}
// 关闭输入流和输出流
fis.close();
fos.close();
System.out.println("图片复制成功!");
} catch (IOException e) {
e.printStackTrace();
}
}
}
java实现:将图片“D:\\test.png”复制到”E:\\pics\\test.png”。
这可以通过IO流进行实现,可以使用FileInputStream读取原图片并创建FileOutputStream对象将图片复制到指定目录,代码如下:
```
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class CopyImage {
public static void main(String[] args) throws IOException {
File sourceFile = new File("D:\\test.png");
File targetFile = new File("E:\\pics\\test.png");
FileInputStream input = null;
FileOutputStream output = null;
try {
input = new FileInputStream(sourceFile);
output = new FileOutputStream(targetFile);
byte[] buffer = new byte[1024];
int len;
while ((len = input.read(buffer)) != -1) {
output.write(buffer, 0, len);
}
System.out.println(sourceFile.getName() + " has been copied to " + targetFile.getAbsolutePath());
} catch (IOException e) {
e.printStackTrace();
} finally {
input.close();
output.close();
}
}
}
```