用Java编程,准备一张图片,使用FileInputStream和FileOutputStream将这张图片复制到其他位置。
时间: 2024-02-19 20:03:06 浏览: 25
好的,我可以帮你解答。以下是Java代码示例:
```java
import java.io.*;
public class CopyImage {
public static void main(String[] args) {
String sourceFilePath = "source.jpg"; // 源图片路径
String destFilePath = "destination.jpg"; // 目标图片路径
try (FileInputStream fis = new FileInputStream(sourceFilePath);
FileOutputStream fos = new FileOutputStream(destFilePath)) {
byte[] buffer = new byte[1024];
int length;
while ((length = fis.read(buffer)) > 0) {
fos.write(buffer, 0, length);
}
System.out.println("图片复制成功!");
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
上述代码中,我们使用了Java的文件输入输出流 FileInputStream 和 FileOutputStream,分别读取源图片和写入目标图片。通过循环读取源图片的字节数据,并将其写入目标图片,实现了图片的复制操作。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)