Java读取指定目录下的所有文件(子目录里的文件也可递归得到)

 1 import java.io.File;
 2 
 3 public class ReadFile {
 4 
 5     public static void main(String[] args) {
 6 
 7         //     path是指定目录的绝对路径
 8         String path = "/Users/tonychan/Workspaces/MyEclipse 2017 CI/Zhangjiajie/WebRoot/pics";
 9         getFile(path);    
10 
11     }
12 
13     // 给定目录的绝对路径,获取该目录下的所有文件(子目录的文件也可递归得到)
14     public static void getFile(String path) {
15         // File对象 可以是文件或者目录
16         File file = new File(path);
17         File[] array = file.listFiles();
18 
19         for (int i = 0; i < array.length; i++) {
20             if (array[i].isFile()) {
21                 // only take file name
22                 System.out.println("^^^^^" + array[i].getName());
23                 // take file path and name
24                 System.out.println("#####" + array[i]);
25                 // take file path and name
26                 System.out.println("*****" + array[i].getPath());
27             } else if (array[i].isDirectory()) {
28                  getFile(array[i].getPath());
29             }
30         }
31     }
32 
33 }

猜你喜欢

转载自www.cnblogs.com/chenhongarticles/p/8995784.html
今日推荐