zoukankan      html  css  js  c++  java
  • 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("输入流关闭失败");
                    }
                }
            }
        }
    }

  • 相关阅读:
    BZOJ 1049: [HAOI2006]数字序列
    BZOJ 1048: [HAOI2007]分割矩阵
    BZOJ 1047: [HAOI2007]理想的正方形
    BZOJ 1046: [HAOI2007]上升序列
    BZOJ 1045: [HAOI2008] 糖果传递
    Flink学习(十三) Flink 常见核心概念分析
    Flink学习(十二) Sink到JDBC(可扩展到任何关系型数据库)
    Flink学习(十一) Sink到Elasticsearch
    Flink学习(十) Sink到Redis
    Flink学习(九) Sink到Kafka
  • 原文地址:https://www.cnblogs.com/BruceKing/p/13541057.html
Copyright © 2011-2022 走看看