zoukankan      html  css  js  c++  java
  • 字节流复制文件

    方法一:
    package cn.lideng.demo2;
    
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    public class Copy {
        public static void main(String[] args) throws FileNotFoundException {
            //建立两个交流对象    绑定数据源和目的地
            FileInputStream fis=null;
            FileOutputStream fos=null;
            
            fis=new FileInputStream("d:\b.txt");
            fos=new FileOutputStream("c:\b.txt");
            
            int len=0;
            try {
                while((len=fis.read())!=-1){
                    fos.write(len);
                }
            } catch (IOException e) {
                System.out.println("文件复制失败");
            }
            finally{
                if(fos!=null){
                    try {
                        fos.close();
                    } catch (Exception e2) {
                        // TODO: handle exception
                    }
                    finally{
                        if(fis!=null){
                            try {
                                fis.close();
                            } catch (Exception e3) {
                                e3.printStackTrace();
                                System.out.println("释放资源失败");
                            }
                        }
                    }
                }
            }
            
        }
    
    }
    
    
    
    
    方法二:
    
    package cn.lideng.demo2;
    
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    public class CopyArr {
        public static void main(String[] args) {
            long c = System.currentTimeMillis();
            FileInputStream fis=null;
            FileOutputStream fos=null;
            
            try {
                fis=new FileInputStream("d:\b.txt");
                fos=new FileOutputStream("c:\b.txt");
                byte[] b=new byte[1024];
                int len=0;
                while((len=fis.read(b))!=-1){
                    fos.write(b,0,len);
                }
                
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }finally{
                if(fos!=null){
                    try {
                        fos.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }finally{
                        if(fis!=null){
                            try {
                                fis.close();
                            } catch (IOException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                                throw new RuntimeException("释放资源失败");
                            }
                        }
                    }
                }
                long e=System.currentTimeMillis();
                System.out.println(e-c);
            }
        }
    
    }
  • 相关阅读:
    【欧拉质数筛选法 模版】
    【归并排序 逆序对 模版】
    【 lca倍增模板】
    【LSGDOJ 1333】任务安排 dp
    【NOIP2013】火柴排队
    【USACO Feb 2014】Cow Decathlon
    【USACO08NOV】奶牛混合起来Mixed Up Cows
    【LSGDOJ 1351】关灯
    【USACO】干草金字塔
    【USACO】电子游戏 有条件的背包
  • 原文地址:https://www.cnblogs.com/qurui1998/p/10595884.html
Copyright © 2011-2022 走看看