2 回答
TA贡献1785条经验 获得超8个赞
You Can use the below code!!
package model;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
public class iterateThroghFolder {
public iterateThroghFolder() {
super();
}
static List<File> file = new ArrayList<File>();
static List<File> list = new ArrayList<File>();;
public static void main(String[] args) {
File file = new File("C:/Users/c_umashe/Desktop/Test"); //Required Path
boolean b = file.isDirectory();
if (b) {
iterateOver(file);
} else{
list.add(file);
}
System.out.println(list.size());
}
public static void iterateOver(File file) {
File[] innerFiles = file.listFiles();
int size = innerFiles.length;
for (int i = 0; i < size; i++) {
if (innerFiles[i].isFile()) {
list.add(innerFiles[i]);
} else { //If it is a directory
iterateOver(innerFiles[i]);
}
}
}
}
TA贡献1831条经验 获得超9个赞
public void listf(String directoryName, List<File> files) {
File directory = new File(directoryName);
// Get all files from a directory.
File[] fList = directory.listFiles();
if(fList != null)
for (File file : fList) {
if (file.isFile()) {
files.add(file);
} else if (file.isDirectory()) {
listf(file.getAbsolutePath(), files);
}
}
}
}
您可以将上述方法调用为
ArrayList<File> f = new ArrayList<File>();
listf("C://Users/xyz", f);
System.out.print(f.size()); // This is the list of files you want.
添加回答
举报