码迷,mamicode.com
首页 > 编程语言 > 详细

【Java】IO流--文件夹的复制

时间:2020-02-23 22:16:54      阅读:77      评论:0      收藏:0      [点我收藏+]

标签:finally   16px   block   oid   class   otf   code   opened   jpg   

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

 

问题分解

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

 

【Java】IO流--文件夹的复制

标签:finally   16px   block   oid   class   otf   code   opened   jpg   

原文地址:https://www.cnblogs.com/syxy/p/12353260.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!