java实现:将图片“D:\\test.png”复制到”E:\\pics\\test.png”。
时间: 2023-06-05 16:06:32 浏览: 79
这可以通过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();
}
}
}
```