FileSearch
import java.io.File;
import java.util.HashSet;
public class FileSearcher {
//Директория для поиска
private final File dir;
//Список папок в директории
private final HashSet<File> listDirectories;
private boolean findSwitch = true;
//Конструктор принимающий путь к директории
public FileSearcher(String dirPath) {
dir = new File(dirPath);
listDirectories = new HashSet<>();
//Получить первые папки для списка
for(File i : dir.listFiles()){
if(i.isDirectory()){
listDirectories.add(i);
}
}
}
public void dirFileWalk(){
for(File dirs : listDirectories){
findSwitch = false;
for(File i : dirs.listFiles()){
if(i.isDirectory() && !listDirectories.contains(i)){
listDirectories.add(i);
findSwitch = true;
}
}
}
}
public HashSet<File> start(){
while(findSwitch)
dirFileWalk();
return listDirectories;
}
}
Main
public class Main {
public static void main(String[] args){
FileSearcher a = new FileSearcher
("c:/Users/Denis/Desktop/pages");
HashSet<File> dirs = a.start();
}
}
Output
Exception in thread "main" java.util.ConcurrentModificationException
at java.util.HashMap$HashIterator.nextNode(HashMap.java:1429)
at java.util.HashMap$KeyIterator.next(HashMap.java:1453)
at com.phaeton.core.FileSearcher.dirFileWalk(FileSearcher.java:28)
at com.phaeton.core.FileSearcher.start(FileSearcher.java:41)
at com.phaeton.starter.Main.main(Main.java:14)
Java Result: 1