2015-11-26 55 views
0

我想遍歷一個文件夾listFiles不返回文件,但

我的文件位於

D:\PROJECT_FOLDER\rootProject\semiRootProject\project\build\resources\main\com\xxxx\pack\file.xlsx 

然而,當我嘗試遍歷它在控制檯只是頂層文件夾它顯示

11:39:06.731 [main] INFO com.xxxx.util.KiePackageCreator - File found: D:\PROJECT_FOLDER\rootProject\semiRootProject\project\build\resources\main\com. 

什麼問題?我的搜索循環看起來像這樣。

File fileFolder = new File(projectBuildDir + RESOURCE_SUBFOLDER); 

for (File file : fileFolder.listFiles(new FilenameFilter() { 

    @Override 
    public boolean accept(File dir, String name) { 
     //if (name.endsWith(".xlsx")) { 
      return true; 
     //} 
     //return false; 
    } 

})) { 
    LOGGER.info("File found: {}.", file.toPath()); 
    if (file.isFile()) { 
     Resource fileResource = getClassPathResource(file.getName()); 
     String filePath = file.getPath(); 
     String rulePath = MAVEN_RESOURCE_PATH + filePath.substring(filePath.indexOf("com")); 
     LOGGER.info("Attempt to write into: {}.", rulePath); 
     kfs.write(rulePath, fileResource); 
    } 
} 
+0

使用遞歸,這就是你需要在這裏 –

回答

2

列表文件列出您指定的目錄中的所有文件和目錄。它不會遞歸地這樣做。

也許walkFileTree適合你更好。

0

您需要遍歷代碼中的子文件夾。您只是遍歷fileFolder指向的目錄中的文件。

相關問題