[Switch] java to get all files in the specified directory

java , how to get all the files in the specified directory?

Look at the code:

Java code    Favorite code
  1. /*** 
  2.      * Get all files (excluding folders) in the specified directory, using recursion 
  3.      *  
  4.      * @param obj 
  5.      * @return 
  6.      */  
  7.     publicstatic ArrayList<File> getListFiles(Object obj) {   
  8.         File directory = null;  
  9.         if (obj instanceof File) {  
  10.             directory = (File) obj;  
  11.         } else {  
  12.             directory = new File(obj.toString());  
  13.         }  
  14.         ArrayList<File> files = new ArrayList<File>();  
  15.         if (directory.isFile()) {  
  16.             files.add(directory);  
  17.             return files;  
  18.         } elseif (directory.isDirectory()) {   
  19.             File[] fileArr = directory.listFiles();  
  20.             for (int i = 0; i < fileArr.length; i++) {  
  21.                 File fileOne = fileArr[i];  
  22.                 files.addAll(getListFiles(fileOne));  
  23.             }  
  24.         }  
  25.         return files;  
  26.     }  

 Note: The above method uses recursion , so it includes files in subdirectories under subdirectories. . .

Test code:

Java code    Favorite code
  1. @Test  
  2.     publicvoid test_getListFiles(){    
  3.         ArrayList<File> files=FileUtils.getListFiles("d:\\Temp\\a\\a");  
  4.         SystemUtil.printFilesFilePath(files);  
  5.     }  

 Output result:

d:\Temp\a\a\divided\merged\oracle study notes.doc

d:\Temp\a\a\divided\oracle study notes.doc_1_3kldv

d:\Temp\a\a\divided\oracle study notes.doc_2_3kldv

d:\Temp\a\a\divided\oracle study notes.doc_3_3kldv

d:\Temp\a\a\oracle study notes.doc

 

Filter prefix ( just get files in the specified directory, no recursion ):

Java code    Favorite code
  1. /*** 
  2.      *  
  3.      * @param path 
  4.      * @param prefixStr 
  5.      * : prefix name 
  6.      * @return 
  7.      */  
  8.     publicstatic File[] getFilesByPathPrefix(File path, final String prefixStr) {   
  9.         File[] fileArr = path.listFiles(new FilenameFilter() {  
  10.             @Override  
  11.             publicboolean accept(File dir, String name) {   
  12.                 // System.out.println("prefixStr:"+prefixStr);  
  13.                 if ((ValueWidget.isNullOrEmpty(prefixStr) || (dir.isDirectory() && name  
  14.                         .startsWith(prefixStr)))) {  
  15.                     returntrue;   
  16.                 } else {  
  17.                     return false;  
  18.                 }  
  19.             }  
  20.         });  
  21.         return fileArr;  
  22.   
  23.     }  
  24. /*** 
  25.      * 前缀名 
  26.      *  
  27.      * @param pathStr 
  28.      * @param prefixStr 
  29.      * @return 
  30.      */  
  31.     public static File[] getFilesByPathAndPrefix(String pathStr,  
  32.             final String prefixStr) {  
  33.         File path = new File(pathStr);  
  34.         return getFilesByPathPrefix(path, prefixStr);  
  35.     }  

 

过滤后缀名(只是获取指定目录下的文件,没有递归):

Java代码    Favorite code
  1. /*** 
  2.      *  
  3.      * @param path 
  4.      * @param prefixStr 
  5.      *            :后缀名 
  6.      * @return 
  7.      */  
  8.     public static File[] getFilesByPathAndSuffix(File path,  
  9.             final String sufixStr) {  
  10.         File[] fileArr = path.listFiles(new FilenameFilter() {  
  11.             @Override  
  12.             public boolean accept(File dir, String name) {  
  13.                 // System.out.println("prefixStr:"+prefixStr);  
  14.                 if ((ValueWidget.isNullOrEmpty(sufixStr) || (dir.isDirectory() && name  
  15.                         .endsWith(sufixStr)))) {  
  16.                     return true;  
  17.                 } else {  
  18.                     return false;  
  19.                 }  
  20.             }  
  21.         });  
  22.         return fileArr;  
  23.   
  24.     }  
  25.   
  26. /*** 
  27.      * 后缀名 
  28.      *  
  29.      * @param pathStr 
  30.      * @param sufixStr 
  31.      * @return 
  32.      */  
  33.     public static File[] getFilesByPathAndSuffix(String pathStr,  
  34.             final String sufixStr) {  
  35.         File path = new File(pathStr);  
  36.         return getFilesByPathAndSuffix(path, sufixStr);  
  37.     }  

 

 

Reprinted from: http://hw1287789687.iteye.com/blog/1946488

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326772307&siteId=291194637