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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值