首页 > 代码库 > 字节流复制一个图片

字节流复制一个图片

1.用字节流读取对象和图片关联

2.用字节流写入流对象,创建一个图片文件.

3.通过循环读写,完成数据存储.

4.关闭资源

 

实例代码:

import java.io.*;class CopyPic {    public static void main(String[] args)     {        FileOutputStream fos = null;        FileInputStream fis = null;        try        {            fos = new FileOutputStream("c:\\2.jpg");            fis = new FileInputStream("c:\\1.jpg");            byte[] buf  = new byte[1024];            int len = 0;            while((len = fis.read(buf)) != -1){                fos.write(buf, 0 , len);            }        }        catch (IOException e)        {            throw new RuntimeException("failed!");        }        finally{            try            {                if(fis !=null)                    fis.close();            }            catch (IOException e)            {                throw new RuntimeException("read failed!");            }            try            {                if(fos !=null)                    fos.close();            }            catch (IOException e)            {                throw new RuntimeException("write failed!");            }        }    }}