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("输入流关闭失败");
                    }
                }
            }
        }
    }

  • 相关阅读:
    Ubuntu虚拟机磁盘空间不足的解决
    eclipse启动报错 JVM terminated. Exit code=1
    Ubuntu16.04 安装eclipse
    HDU 1710 Binary Tree Traversals(二叉树)
    Ubuntu16.04 搭建伪分布式Hadoop环境
    HDU 1560 DNA sequence(IDA*)
    Go的函数
    Go的包
    Go语言开发环境搭建
    go的循环
  • 原文地址:https://www.cnblogs.com/BruceKing/p/13541057.html
Copyright © 2011-2022 走看看