IO查找文件复制到指定路径

 1 public class IOTest {
 2     // 存储获取的文件绝对路径
 3     private static List<String> list = new ArrayList<String>();
 4     public static void main(String[] args) {
 5         File file = new File("D:/holle");
 6         Long sta = System.currentTimeMillis();
 7         mkdirAll(file);
 8         for(String str : list){
 9             String[] split = str.split("\\\\");
10             try {
11                 IO(str, "D:/"+split[split.length-1]);
12             } catch (IOException e) {
13                 e.printStackTrace();
14             }
15         }
16         Long end = System.currentTimeMillis();
17         System.out.println("用时"+(end-sta));
18     }
19     
20     
21     private static void mkdirAll(File file){
22         File[] f = file.listFiles();
23         for (File file2 : f) {
24             // 递归算法,如果有子文件夹继续向下查找
25             if(file2.isDirectory()){
26                 mkdirAll(file2);
27             }else{
28                 list.add(file2.toString());
29             }
30         }
31     }
32     
33     public static void IO(String file,String newFile)throws IOException{
34         //
35         File f1 = new File(file);
36         File f2 = new File(newFile);
37         //
38         FileInputStream fis = new FileInputStream(f1);
39         FileOutputStream fos = new FileOutputStream(f2);
40         //
41         byte[] data = new byte[1024 * 1024];
42         //
43         int len = 0;
44         while((len = fis.read(data)) != -1){
45             fos.write(data, 0, len);
46         }
47         //
48         fis.close();
49         fos.close();    
50     }
51 }
原文地址:https://www.cnblogs.com/cypress/p/7838355.html