1 public class FileTest {
2 // 存储获取的文件绝对路径
3 private static List list = new ArrayList<String>();
4
5 public static void main(String[] args) {
6 File f = new File("E:/111");
7 List<String> fileWi = fileWi(f);
8 for (String str : fileWi) {
9 String[] split = str.split("\\\\");
10 try {
11 fileIO(str, "E:/111/" + split[split.length - 1]);
12 } catch (IOException e) {
13 e.printStackTrace();
14 }
15 }
16 System.out.println("or");
17 }
18
19 public static List fileWi(File file) {
20 // 递归算法,如果有子文件夹继续向下查找
21 File[] files = file.listFiles();
22 for (File file2 : files) {
23 if (file2.isDirectory()) {
24 fileWi(file2);
25 } else {
26 list.add(file2.toString());
27 }
28 }
29 return list;
30 }
31
32 public static void fileIO(String f1, String f2) throws IOException {
33 // 源
34 File file = new File(f1);
35 File fileNew = new File(f2);
36 // 流
37 FileInputStream fis = new FileInputStream(file);
38 FileOutputStream fos = new FileOutputStream(fileNew);
39 // 读
40 byte[] data = new byte[1024 * 1024];
41 // 写
42 int len = 0;
43 while ((len = fis.read(data)) != -1) {
44 fos.write(data, 0, len);
45 }
46 // 关
47 fis.close();
48 fos.close();
49 }
50 }
递归算法的返回值每次只能返回一个,需要全局变量的配合来存储数据