复制文件夹里面的文件
直接上代码:
1 package com.hw.file0223;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.nio.file.Files;
6
7 import org.junit.Test;
8
9 public class PracticeTest03 {
10 @Test
11 public void test03(){
12 File sourceFolder = null;
13 File targetFolder = null;
14
15 sourceFolder = new File("F://骚操作");
16 targetFolder = new File("F://骚操作附件");
17 File[] files = sourceFolder.listFiles(); //取得里面的文件并返回一个File数组
18
19 for(File file : files)
20 {
21 if(file.isDirectory()) continue;
22 File newFiles = new File(targetFolder,file.getName());
23 try {
24 Files.copy(sourceFolder.toPath(), newFiles.toPath());
25 } catch (IOException e) {
26 // TODO Auto-generated catch block
27 e.printStackTrace();
28 }
29 }
30 }
31 }
主要思路就是,先用listFiles方法取得所有文件,然后存到File数组里面,接着遍历这个数组,使用File里面的parent,child构造方法,然后使用copy方法,就可以了。

浙公网安备 33010602011771号