ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

Java IO流--使用FileInputStream和FileOutputStream复制图片和文件

2021-06-21 22:32:38  阅读:123  来源: 互联网

标签:fis Java fos -- printStackTrace FileOutputStream File new null


一、复制图片

 @Test
    public void test2(){
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            //1.提供File类的对象
            File file1 = new File("工作二维码.png");
            File file2 = new File("工作二维码3.png");

            //2.造流
            fis = new FileInputStream(file1);
            fos = new FileOutputStream(file2);

            //3.复制图片
            byte[] buffer = new byte[5];
            int len;
            while ((len=fis.read(buffer))!=-1){
                fos.write(buffer,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //4.关闭资源
            if (fis!=null){
                try {
                    fis.close();
                }catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fos!=null){
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

二、复制文件

//指定路径下文件的复制
    public void copyFile(String srcPath,String descPath){
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            //1.提供File类的对象
            File file1 = new File(srcPath);
            File file2 = new File(descPath);

            //2.造流
            fis = new FileInputStream(file1);
            fos = new FileOutputStream(file2);

            //3.复制图片
            byte[] buffer = new byte[1024];
            int len;
            while ((len=fis.read(buffer))!=-1){
                fos.write(buffer,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //4.关闭资源
            if (fis!=null){
                try {
                    fis.close();
                }catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fos!=null){
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    @Test
    public void testCopyFile(){
        long start = System.currentTimeMillis();

        String srcPath="C:\\Users\\DELL\\Desktop\\1.回顾类-封装和继承.mp4";
        String descPath= "C:\\Users\\DELL\\Desktop\\1.回顾类-封装和继承3.mp4";

        copyFile(srcPath,descPath);

        long end = System.currentTimeMillis();

        System.out.println("复制消耗的时间:"+(end-start));
    }

标签:fis,Java,fos,--,printStackTrace,FileOutputStream,File,new,null
来源: https://blog.csdn.net/lu202032/article/details/118095212

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有