R
R
Rienopely2015-09-16 07:29:04
Java
Rienopely, 2015-09-16 07:29:04

Why does it throw an exception when traversing a too deep directory?

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: one

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Danil Antoshkin, 2015-09-16
@rienopely

Can you write a mistake?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question