zoukankan      html  css  js  c++  java
  • 【Java】IO流--文件夹的复制

    目标:复制一个文件夹下所有的文件及目录

    问题分解

    1.复制一个文件到指定位置

    2.复制指定目录下的所有文件到指定位置

    3.复制指定目录下所有是文件及目录到指定位置

    代码

    1.复制一个文件到指定位置

     1 import java.io.BufferedInputStream;
     2 import java.io.BufferedOutputStream;
     3 import java.io.File;
     4 import java.io.FileInputStream;
     5 import java.io.FileNotFoundException;
     6 import java.io.FileOutputStream;
     7 import java.io.IOException;
     8 
     9 public class TestCopyFile {
    10     public static void copyOneFile(File src,File dest) {
    11         BufferedInputStream bis=null;
    12         BufferedOutputStream bos=null;
    13         try {
    14             bis = new BufferedInputStream(new FileInputStream(src));
    15             bos = new BufferedOutputStream(new FileOutputStream(dest));
    16             
    17             byte[] buf=new byte[1024];
    18             int len=-1;
    19             
    20             while((len=bis.read(buf))!=-1) {
    21                 bos.write(buf, 0, len);
    22             }
    23         } catch (FileNotFoundException e) {
    24             // TODO Auto-generated catch block
    25             e.printStackTrace();
    26         } catch (IOException e) {
    27             // TODO Auto-generated catch block
    28             e.printStackTrace();
    29         }finally {
    30             if(bos!=null) {
    31                 try {
    32                     bos.close();
    33                 } catch (IOException e) {
    34                     // TODO Auto-generated catch block
    35                     e.printStackTrace();
    36                 }
    37             }
    38             if(bis!=null) {
    39                 try {
    40                     bis.close();
    41                 } catch (IOException e) {
    42                     // TODO Auto-generated catch block
    43                     e.printStackTrace();
    44                 }
    45             }
    46         }
    47     }
    48     
    49     public static void main(String[] args) {
    50         File src=new File("D:\Test\恋する彼女の不器用な舞台 Visual Fan Book (JPG).zip");
    51         File dest=new File("F:\恋する彼女の不器用な舞台 Visual Fan Book (JPG).zip");
    52         copyOneFile(src, dest);
    53     }
    54 }
    View Code

    2.复制指定目录下的所有文件到指定位置

     1 import java.io.BufferedInputStream;
     2 import java.io.BufferedOutputStream;
     3 import java.io.File;
     4 import java.io.FileInputStream;
     5 import java.io.FileNotFoundException;
     6 import java.io.FileOutputStream;
     7 import java.io.IOException;
     8 
     9 public class TestCopyFile {
    10     public static void copyOneFile(File src,File dest) {
    11         BufferedInputStream bis=null;
    12         BufferedOutputStream bos=null;
    13         try {
    14             bis = new BufferedInputStream(new FileInputStream(src));
    15             bos = new BufferedOutputStream(new FileOutputStream(dest));
    16             
    17             byte[] buf=new byte[1024];
    18             int len=-1;
    19             
    20             while((len=bis.read(buf))!=-1) {
    21                 bos.write(buf, 0, len);
    22             }
    23         } catch (FileNotFoundException e) {
    24             // TODO Auto-generated catch block
    25             e.printStackTrace();
    26         } catch (IOException e) {
    27             // TODO Auto-generated catch block
    28             e.printStackTrace();
    29         }finally {
    30             if(bos!=null) {
    31                 try {
    32                     bos.close();
    33                 } catch (IOException e) {
    34                     // TODO Auto-generated catch block
    35                     e.printStackTrace();
    36                 }
    37             }
    38             if(bis!=null) {
    39                 try {
    40                     bis.close();
    41                 } catch (IOException e) {
    42                     // TODO Auto-generated catch block
    43                     e.printStackTrace();
    44                 }
    45             }
    46         }
    47     }
    48     
    49     public static void copyAllFiles(File src,File dest) {
    50         if(!dest.exists()) {
    51             dest.mkdir();
    52         }
    53         File[] flies=src.listFiles();
    54         for (File file : flies) {
    55             if(file.isFile()) {
    56                 copyOneFile(new File(src+"\"+file.getName()) ,new File(dest+"\"+file.getName()));
    57             }
    58         }
    59     }
    60     
    61     public static void main(String[] args) {
    62         File src=new File("D:\Test");
    63         File dest=new File("F:\Test");
    64         copyAllFiles(src, dest);
    65     }
    66 }
    View Code

    3.复制指定目录下所有是文件及目录到指定位置

     1 import java.io.BufferedInputStream;
     2 import java.io.BufferedOutputStream;
     3 import java.io.File;
     4 import java.io.FileInputStream;
     5 import java.io.FileNotFoundException;
     6 import java.io.FileOutputStream;
     7 import java.io.IOException;
     8 
     9 public class TestCopyFile {
    10     public static void copyOneFile(File src,File dest) {
    11         BufferedInputStream bis=null;
    12         BufferedOutputStream bos=null;
    13         try {
    14             bis = new BufferedInputStream(new FileInputStream(src));
    15             bos = new BufferedOutputStream(new FileOutputStream(dest));
    16             
    17             byte[] buf=new byte[1024];
    18             int len=-1;
    19             
    20             while((len=bis.read(buf))!=-1) {
    21                 bos.write(buf, 0, len);
    22             }
    23         } catch (FileNotFoundException e) {
    24             // TODO Auto-generated catch block
    25             e.printStackTrace();
    26         } catch (IOException e) {
    27             // TODO Auto-generated catch block
    28             e.printStackTrace();
    29         }finally {
    30             if(bos!=null) {
    31                 try {
    32                     bos.close();
    33                 } catch (IOException e) {
    34                     // TODO Auto-generated catch block
    35                     e.printStackTrace();
    36                 }
    37             }
    38             if(bis!=null) {
    39                 try {
    40                     bis.close();
    41                 } catch (IOException e) {
    42                     // TODO Auto-generated catch block
    43                     e.printStackTrace();
    44                 }
    45             }
    46         }
    47     }
    48     
    49     public static void copyAllFiles(File src,File dest) {
    50         if(!dest.exists()) {
    51             dest.mkdir();
    52         }
    53         File[] flies=src.listFiles();
    54         for (File file : flies) {
    55             if(file.isFile()) {
    56                 copyOneFile(new File(src+"\"+file.getName()) ,new File(dest+"\"+file.getName()));
    57             }else {
    58                 copyAllFiles(new File(src+"\"+file.getName()) ,new File(dest+"\"+file.getName()));
    59             }
    60         }
    61     }
    62     
    63     public static void main(String[] args) {
    64         File src=new File("D:\Test");
    65         File dest=new File("F:\Test");
    66         copyAllFiles(src, dest);
    67     }
    68 }
    View Code
  • 相关阅读:
    c语言数组指针
    (4)activiti工作流引擎之uel表达式
    (3)activiti流程的挂起和激活
    (2)java程序走一遍工作流activiti
    (1)activiti认识以及数据库和插件配置
    linux 下路由配置
    lvs-dr+keepalived
    LVS-DR 配置测试
    简单认识TCP/IP协议
    mysql 主从同步-读写分离
  • 原文地址:https://www.cnblogs.com/syxy/p/12353260.html
Copyright © 2011-2022 走看看