利用IO流复制图片

import java.io.*;

public class CopyPicture {
    public static void main(String[] args) {
        //创建文件图片路径
        File i = new File("C:\\Users\\Administrator\\Desktop\\picture\\a.png");//要拷贝的文件
        File o = new File("C:\\Users\\Administrator\\Desktop\\picture\\b.png");//拷贝完的图片
        //创建流
        InputStream is = null;//输入流
        OutputStream os = null;//输出流
        try {
            is = new FileInputStream(i);
            os = new FileOutputStream(o);
            //拷贝的长度
            int len = -1;
            //缓存容器
            byte[] flush = new byte[1024];
            //从目标源读出
            while ((len = is.read(flush)) != -1) {
                //写入
                os.write(flush);
                //刷新
                os.flush();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {

            //关闭输出流
            if (null != os) {
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            //关闭输入流
            if (null != is) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }


    }
}

 

posted @ 2020-06-10 19:38  技术白菜  阅读(284)  评论(0编辑  收藏  举报