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

  • 相关阅读:
    Zigbee学习路线
    Zigbee简介
    验证lagrange 定理
    为什么(12)式,km不能直接相乘?而要让域k先乘一个代数A里面的单位元,再作用在群M上呢?
    strong weak distribution
    sufficient statistics
    tensorflow TypeError: Can not convert a float32 into a Tensor or Operation
    tensorflow 训练的时候loss=nan
    tensorflow run()和 eval()
    python array基本操作一
  • 原文地址:https://www.cnblogs.com/BruceKing/p/13541057.html
Copyright © 2011-2022 走看看