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

  • 相关阅读:
    python学习之模块补充二
    MySQL的表关系
    初识数据库
    MySQL基础
    死锁 递归锁 信号量 Event事件 线程q
    进程池/线程池与协程
    线程
    进程相关知识点
    python 之多进程
    socket 基础
  • 原文地址:https://www.cnblogs.com/BruceKing/p/13541057.html
Copyright © 2011-2022 走看看