欢迎来到萧静默的博客

书山有路勤为径,学海无涯苦作舟。

Java-IO流-文件复制1

package cn.bruce.IO;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class IOCopyDemo {
    public static void main(String[] args) throws IOException {
        FileInputStream Zfin = null;// 输入流
        FileOutputStream Zfou = null;// 输出流
        FileInputStream Zfin1 = null;// 输入流
        FileOutputStream Zfou1 = null;// 输出流
        Zfin = new FileInputStream("E:\\A\\aa.zip");// 绑定数据源66,076,638 字节63.0 MB
        Zfou = new FileOutputStream("E:\\A\\abcc.zip");// 绑定数据目的
        Zfin1 = new FileInputStream("E:\\A\\aa.zip");// 绑定数据源66,076,638 字节63.0 MB
        Zfou1 = new FileOutputStream("E:\\A\\abccdd.zip");// 绑定数据目的
        long s = System.currentTimeMillis();
        fun(Zfin, Zfou);// 一个一个字节的复制,太慢了
        long e = System.currentTimeMillis();
        System.out.println(e - s);
        long s1 = System.currentTimeMillis();
        fun1(Zfin1, Zfou1);// 数组形式的复制
        long e1 = System.currentTimeMillis();
        System.out.println(e1 - s1);
    }

    public static void fun(FileInputStream fin, FileOutputStream fou) {
        try
        {
            int len = 0;
            // System.out.println(fin.read());
            while ((len = fin.read()) != -1)
            {
                // System.out.println((char) len);
                fou.write((char) len);
            }
        } catch (IOException e)
        {
            e.printStackTrace();
            throw new RuntimeException("文件复制失败");
        } finally
        {
            try
            {
                if (fou != null)
                {
                    fou.close();// 关输出流
                }
            } catch (IOException e2)
            {
                e2.printStackTrace();
                throw new RuntimeException("输出流关闭失败");
            } finally
            {
                try
                {
                    if (fin != null)
                    {
                        fin.close();// 关输入流
                    }
                } catch (IOException e2)
                {
                    e2.printStackTrace();
                    throw new RuntimeException("输入流关闭失败");
                }
            }
        }
    }

    public static void fun1(FileInputStream fin, FileOutputStream fou) {
        try
        {
            byte[] bs = new byte[1024];
            int len = 0;
            // System.out.println(fin.read());
            while ((len = fin.read(bs)) != -1)
            {
                // System.out.println(new String(bs, 0, len));
                fou.write(bs, 0, len);
            }
        } catch (IOException e)
        {
            e.printStackTrace();
            throw new RuntimeException("文件复制失败");
        } finally
        {
            try
            {
                if (fou != null)
                {
                    fou.close();// 关输出流
                }
            } catch (IOException e2)
            {
                e2.printStackTrace();
                throw new RuntimeException("输出流关闭失败");
            } finally
            {
                try
                {
                    if (fin != null)
                    {
                        fin.close();// 关输入流
                    }
                } catch (IOException e2)
                {
                    e2.printStackTrace();
                    throw new RuntimeException("输入流关闭失败");
                }
            }
        }
    }
}

 

posted @ 2020-08-21 14:54  萧静默  阅读(123)  评论(0编辑  收藏  举报