zoukankan      html  css  js  c++  java
  • 字节流的4种复制方式

    package day21_io.test;
    
    import java.io.BufferedInputStream;
    import java.io.BufferedOutputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    /**
     * 复制图片
     * 
     * 分析:
     *         复制数据,如果我们知道用记事本打开并能够读懂,就用字符流,否则用字节流。
     *         通过该原理,我们知道我们应该采用字节流。
     *         而字节流有4种方式,所以做这个题目我们有4种方式。推荐掌握第4种。
     * 数据源:
     *         c:\a.jpg -- FileInputStream -- BufferedInputStream
     * 目的地:
     *         d:\b.jpg -- FileOutputStream -- BufferedOutputStream
     * @author chenwen
     *
     */
    public class Test8 {
        
        public static void main(String[] args) throws IOException {
            File srcFile =new File("c:\a.png");
            File destFile =new File("d:\d.png");
            
    //        method1(srcFile,destFile);
    //        method2(srcFile,destFile);
    //        method3(srcFile,destFile);
            method4(srcFile,destFile);
        }
    //    使用字节缓冲流读取一个字节数组
        private static void method4(File srcFile, File destFile) throws IOException {
            
            BufferedInputStream bis =new BufferedInputStream(new FileInputStream(srcFile));
            BufferedOutputStream bos =new BufferedOutputStream(new FileOutputStream(destFile));
    //        字节流,使用字节数组
            byte[] bys =new byte[1024];
            int len =0;
    //        read里面的参数要注意
            while((len=bis.read(bys))!=-1) {
                bos.write(bys,0,len);
    //            bos.flush();
            }
            bos.close();
            bis.close();
        }
    //    使用字节缓冲流读取一个字节
        private static void method3(File srcFile, File destFile) throws IOException {
            
            BufferedInputStream bis =new BufferedInputStream(new FileInputStream(srcFile));
            BufferedOutputStream bos =new BufferedOutputStream(new FileOutputStream(destFile));
            
            int ch =0;
            while((ch=bis.read())!=-1) {
                bos.write(ch);;
            }
            bos.close();
            bis.close();
        }
    //    使用基本字节流读取一个字节数组
        private static void method2(File srcFile, File destFile) throws IOException {
            FileInputStream fis =new FileInputStream(srcFile);
            FileOutputStream fos =new FileOutputStream(destFile);
            byte[] bys=new byte[1024];
            int len =0;
            while((len=fis.read(bys))!=-1) {
                fos.write(bys,0,len);;
            }
            fos.close();
            fis.close();
        }
    //    使用基本字节流读取一个字节
        private static void method1(File srcFile, File destFile) throws IOException {
            FileInputStream fis =new FileInputStream(srcFile);
            FileOutputStream fos =new FileOutputStream(destFile);
            int ch =0;
            while((ch=fis.read())!=-1) {
                fos.write(ch);;
            }
            fos.close();
            fis.close();
        }
    
    }
  • 相关阅读:
    周五笔记
    python2.0代码重构为3.0一日记
    小白使用Bert跑分类模型
    andrew ng 深度学习 网易云课堂课程
    andrew ng machine learning week9 异常检测和推荐系统
    解决端口占用问题
    postgresqlmysql删除数据表中字段的回车和换行
    echarts常用的属性修改
    后端返回文件流,用blog方式下载 type 值
    antD vue 遇到的一些问题处理
  • 原文地址:https://www.cnblogs.com/csslcww/p/9179483.html
Copyright © 2011-2022 走看看