用java编程实现:将图片“D:\\test.png”复制到”E:\\pics\\test.png”
时间: 2023-06-06 16:06:08 浏览: 57
好的,我可以回答这个问题。你可以参考下面的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();
}
}
}