您的位置:首页 > 其它

IO流--拷贝图片

2015-12-02 07:55 337 查看
/*
需求:复制一个图片。
思路:
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());
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: