IO流(最简单的图片拷贝)

之前在学习io流的时候遇到了一些困难,看视频,看书,总感觉自己会了实际去写的时候,不知道该怎么写,下面写了一段自认为比较简单的io流应用,新手可以学习一下,大佬勿喷


        package com.arom.io;

        import java.io.File;
        import java.io.FileInputStream;
        import java.io.FileNotFoundException;
        import java.io.FileOutputStream;
        import java.io.IOException;
        import java.io.InputStream;
        import java.io.OutputStream;
        public class IOcopy {

  public static void main(String[] args) {
    //创建文件图片路径
    File i = new File("a.jpg");//要拷贝的文件
    File o = new File("b.jpg");//拷贝完的图片
    //创建流
    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();
            }
        }
    }
    
}

}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容