/*
需求:复制一个图片。
思路:
1.用字节读取流对象和图片关联。
2.用字节写入流对象创建一个图片文件,用于存储获取到的图片数据。
3.通过循环写,完成数据的存储。
4.关闭资源。
*/
import java.io.*;
class CopyPicture{
public static void main(String[] args) {
FileInputStream fis = null;
FileOutputStream fos = null;
try{
fis = new FileInputStream("C:\\1.jpg");
fos = new FileOutputStream("C:\\2.jpg");
byte[] buffer = new byte[1024];
int length = 0;
while((length = fis.read(buffer)) != -1){
fos.write(buffer,0,length);
}
}catch (IOException e){
try {
throw new IOException("复制文件失败");
} catch (IOException e1) {
e1.printStackTrace();
}
}finally{
try{
if(fis != null)
fis.close();
}catch (IOException e){
System.out.println(e.toString());
}
try{
if(fos != null)
fos.close();
}catch (IOException e){
System.out.println(e.toString());
}
}
}
}
IO流--拷贝图片
最新推荐文章于 2024-06-28 01:24:53 发布