有没有办法在java的帮助下列出特定文件夹中的所有文档,包括来自所有子文件夹中的文件?

我想获取文件夹中存在的所有文档的列表。包括文件夹的子文件夹中存在的那些。目前,我只能编写代码,循环访问文件夹中存在的文档,而不能循环访问其子文件夹。


     File dir = new File("C:\\Users\\...\\Forms");

     File[] list = dir.listFiles();

     for(int i=0; i<list.length;i++)

     {

     ...

     }

是否有Java代码可以从文件夹中获取所有文档,包括其子文件夹?


千巷猫影
浏览 66回答 2
2回答

慕的地10843

You Can use the below code!!package model;import java.io.File;import java.util.ArrayList;import java.util.List;public class iterateThroghFolder {&nbsp; &nbsp; public iterateThroghFolder() {&nbsp; &nbsp; &nbsp; &nbsp; super();&nbsp; &nbsp; }&nbsp; &nbsp; static List<File> file = new ArrayList<File>();&nbsp; &nbsp; static List<File> list = new ArrayList<File>();;&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; File file = new File("C:/Users/c_umashe/Desktop/Test"); //Required Path&nbsp; &nbsp; &nbsp; &nbsp; boolean b = file.isDirectory();&nbsp; &nbsp; &nbsp; &nbsp; if (b) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; iterateOver(file);&nbsp; &nbsp; &nbsp; &nbsp; } else{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;list.add(file);&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(list.size());&nbsp; &nbsp; }&nbsp; &nbsp; public static void iterateOver(File file) {&nbsp; &nbsp; &nbsp; &nbsp; File[] innerFiles = file.listFiles();&nbsp; &nbsp; &nbsp; &nbsp; int size = innerFiles.length;&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < size; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (innerFiles[i].isFile()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; list.add(innerFiles[i]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else { //If it is a directory&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; iterateOver(innerFiles[i]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}

天涯尽头无女友

&nbsp;public void listf(String directoryName, List<File> files) {&nbsp; &nbsp; &nbsp; &nbsp; File directory = new File(directoryName);&nbsp; &nbsp; &nbsp; &nbsp; // Get all files from a directory.&nbsp; &nbsp; &nbsp; &nbsp; File[] fList = directory.listFiles();&nbsp; &nbsp; &nbsp; &nbsp; if(fList != null)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (File file : fList) {&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (file.isFile()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; files.add(file);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else if (file.isDirectory()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; listf(file.getAbsolutePath(), files);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }您可以将上述方法调用为&nbsp; &nbsp; &nbsp; &nbsp; ArrayList<File> f = new ArrayList<File>();&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; listf("C://Users/xyz", f);&nbsp; &nbsp; &nbsp; &nbsp; System.out.print(f.size()); // This is the list of files you want.
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java