Java reads all files in the specified directory (files in subdirectories can also be obtained recursively)

1  import java.io.File;
 2  
3  public  class ReadFile {
 4  
5      public  static  void main(String[] args) {
 6  
7          //      path is the absolute path of the specified directory 
8          String path = "/Users/tonychan/Workspaces/ MyEclipse 2017 CI/Zhangjiajie/WebRoot/pics" ;
 9          getFile(path);    
 10  
11      }
 12  
13      // Given the absolute path of a directory, get all the files in the directory (the files in subdirectories can also be obtained recursively) 
14      public  static  void getFile(String path) {
 15          // The File object can be a file or a directory
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 }

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325338351&siteId=291194637