图片复制java简单代码
时间: 2023-08-22 21:10:20 浏览: 43
当然可以!以下是一个简单的Java代码示例,用于将一张图片复制到另一个位置:
```java
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class ImageCopyExample {
public static void main(String[] args) {
File sourceFile = new File("path/to/source/image.jpg");
File destinationFile = new File("path/to/destination/image.jpg");
try {
FileInputStream inputStream = new FileInputStream(sourceFile);
FileOutputStream outputStream = new FileOutputStream(destinationFile);
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
inputStream.close();
outputStream.close();
System.out.println("图片复制成功!");
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
请将 `"path/to/source/image.jpg"` 替换为源图片的路径,将 `"path/to/destination/image.jpg"` 替换为目标位置的路径。运行该代码后,源图片将会被复制到目标位置。
希望这个简单的示例能帮到你!如果你有任何其他问题,请随时问我。