IO P05 复制图片 (字节流)

抛出异常的处理方法 

package file;

import java.io.*;

// P05 复制图片
public class TestCopyImg {
    public static void main(String[] args) throws IOException {
        long newTime = System.currentTimeMillis();
        // 源文件目录
        String srcImgPath = "/Users/hike/Desktop/java/JavaSE/1.png";
        // 目标文件目录
        String distImgPath = "/Users/hike/Desktop/java/JavaSE/aa/bb/cc/1.png";
        InputStream fis = new FileInputStream(srcImgPath);
        OutputStream fos = new FileOutputStream(distImgPath);

        // 每读一个就去写一次,很麻烦,低效
//        int read;
//        while((read = fis.read()) != -1){
//            fos.write(read);
//        }

        // 一般情况,我们定义这个bytes的时候,长度要定义为1024的整数倍
        byte[] bytes = new byte[10];
        int len;
        StringBuilder sb = new StringBuilder();
        while ((len = fis.read(bytes)) != -1) {
            fos.write(bytes, 0, len);
        }

        fis.close();
        fos.close();
        System.out.println("一共经历了:" + (System.currentTimeMillis() - newTime) + " 毫秒");
    }
}

用 一个字节一个字节的方式 复制图片,速度慢

如果我们用读取字节数组的方式复制图片,速度快,效率高,比较吃内存

捕获异常,异常代码的处理

package file;

import java.io.*;

// P05 复制图片  不抛出异常,捕获异常
public class TestCopyImg2 {
    public static void main(String[] args)  {
        long newTime = System.currentTimeMillis();
        // 源文件目录
        String srcImgPath = "/Users/hike/Desktop/java/JavaSE/2.png";
        // 目标文件目录
        String distImgPath = "/Users/hike/Desktop/java/JavaSE/aa/bb/cc/1.png";

        InputStream fis = null;
        OutputStream fos = null;

        // 一般情况,我们定义这个bytes的时候,长度要定义为1024的整数倍
        byte[] bytes = new byte[10];
        int len;
        StringBuilder sb = new StringBuilder();
        try {
            fis = new FileInputStream(srcImgPath);
            fos = new FileOutputStream(distImgPath);

            while ((len = fis.read(bytes)) != -1) {
                fos.write(bytes, 0, len);
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fis != null) {
                    fis.close();
                }
                if (fos != null) {
                    fos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                fis = null;
                fos = null;
            }
        }


        System.out.println("一共经历了:" + (System.currentTimeMillis() - newTime) + " 毫秒");
    }
}

再次优化

package file;

import java.io.*;

// P05 复制图片  不抛出异常,捕获异常 优化代码  java7之后  引入了 try-with-resource语句,默认执行到之后,自动执行流的关闭命令
public class TestCopyImg3 {
    // 测试
    private static class MyInputStream extends FileInputStream{
        public MyInputStream(String name) throws FileNotFoundException {
            super(name);
        }
        public MyInputStream(File file) throws FileNotFoundException {
            super(file);
        }
        public MyInputStream(FileDescriptor fdObj) {
            super(fdObj);
        }

        @Override
        public void close() throws IOException {
            super.close();
            System.out.println("MyInputStream 的关闭方法被调用");
        }
    }
    private static class MyOutputStream extends FileOutputStream{
        public MyOutputStream(String name) throws FileNotFoundException {
            super(name);
        }
        public MyOutputStream(String name, boolean append) throws FileNotFoundException {
            super(name, append);
        }
        public MyOutputStream(File file) throws FileNotFoundException {
            super(file);
        }
        public MyOutputStream(File file, boolean append) throws FileNotFoundException {
            super(file, append);
        }
        public MyOutputStream(FileDescriptor fdObj) {
            super(fdObj);
        }
        @Override
        public void close() throws IOException {
            super.close();
            System.out.println("MyOutputStream 的关闭方法被调用");
        }
    }

    public static void main(String[] args)  {

        long newTime = System.currentTimeMillis();
        // 源文件目录
        String srcImgPath = "/Users/hike/Desktop/java/JavaSE/2.png";
        // 目标文件目录
        String distImgPath = "/Users/hike/Desktop/java/JavaSE/aa/bb/cc/2.png";


        // 一般情况,我们定义这个bytes的时候,长度要定义为1024的整数倍
        byte[] bytes = new byte[10];
        int len;

        try(InputStream fis = new MyInputStream(srcImgPath);OutputStream fos = new MyOutputStream(distImgPath);) {
            while ((len = fis.read(bytes)) != -1) {
                fos.write(bytes, 0, len);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


        System.out.println("一共经历了:" + (System.currentTimeMillis() - newTime) + " 毫秒");
    }
}

简化下来其实就是这样

package file;

import java.io.*;

// P05 复制图片  不抛出异常,捕获异常 优化代码  java7之后  引入了 try-with-resource语句,默认执行到之后,自动执行流的关闭命令
public class TestCopyImg3 {
    public static void main(String[] args)  {
        long newTime = System.currentTimeMillis();
        // 源文件目录
        String srcImgPath = "/Users/hike/Desktop/java/JavaSE/2.png";
        // 目标文件目录
        String distImgPath = "/Users/hike/Desktop/java/JavaSE/aa/bb/cc/2.png";
        
        // 一般情况,我们定义这个bytes的时候,长度要定义为1024的整数倍
        byte[] bytes = new byte[10];
        int len;

        try(InputStream fis = new FileInputStream(srcImgPath);OutputStream fos = new FileOutputStream(distImgPath);) {
            while ((len = fis.read(bytes)) != -1) {
                fos.write(bytes, 0, len);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        System.out.println("一共经历了:" + (System.currentTimeMillis() - newTime) + " 毫秒");
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值