Java进阶(三十七)IO流5:FileInputStream、FileOutputStream

一、小结

  • 对于文本文件(txt,java ,c,cpp),使用字符流处理。
  • 对于非文本文件(jpg,mp3,mp4,avi,doc,ppt),使用字节流处理。

二、使用FileInputStream和FileOutputStream复制非文本文件

2.1步骤

同之前。

    /**
     * 实现对图片的复制
     */
    @Test
    public void testFileInputOutputStream() {
        //1.实例化file对象
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            File srcfile = new File("photomode.png");
            File destfile = new File("photomode2.png");

            //2.实例化流对象
            fis = new FileInputStream(srcfile);
            fos = new FileOutputStream(destfile);

            //3.读写数据
            byte[] bbuff = new byte[1024];
            int len;
            while ((len = fis.read(bbuff)) != -1) {
                fos.write(bbuff, 0, len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //4.关闭流
            try {
                if (fis != null)
                    fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (fos != null)
                    fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

三、封装成复制方法

    //指定路径下文件的复制
    public void copyFile(String srcPath, String destPath) {
        //1.实例化file对象
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            File srcfile = new File(srcPath);
            File destfile = new File(destPath);

            //2.实例化流对象
            fis = new FileInputStream(srcfile);
            fos = new FileOutputStream(destfile);

            //3.读写数据
            byte[] bbuff = new byte[1024];
            int len;
            while ((len = fis.read(bbuff)) != -1) {
                fos.write(bbuff, 0, len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //4.关闭流
            try {
                if (fis != null)
                    fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (fos != null)
                    fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Test
    public void testCopyFile() {
        long start = System.currentTimeMillis();

        String srcPath="E:\\developer\\idea\\workspace\\JavaSenior\\09.IO\\01-video.mp4";
        String destPath="E:\\developer\\idea\\workspace\\JavaSenior\\09.IO\\02-video.mp4";

        copyFile(srcPath,destPath);

        long end = System.currentTimeMillis();
        System.out.println("复制操作花费的时间:" + (end - start));//1477
    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值