I'm on JAVA in general 0. Therefore, immediately apologize if something is not so described)

There was a need to modify one piece of code:

private static Map<String, String> getClassPropertyAnnotations(PhpModule pm) { Map<String, String> annotations = new HashMap(); String parentPath = pm.getSourceDirectory().getPath(); String[] paths = CIPreferences.getCustomLibraryPaths(pm); for (String path : paths) { File directory = new File(path); if (!directory.isAbsolute()) { directory = new File(parentPath, path); } if ((directory.exists()) && (directory.isDirectory())) { for (File file : directory.listFiles()) { if (file.getName().endsWith(".php")) { String fileName = file.getName().replace(".php", ""); String className = ucfirst(fileName); String propertyName = lcfirst(fileName); annotations.put(className, propertyName); } } } } return annotations; } 

Here we are interested in a piece of work with directories. It turns out this way: directories are transferred, they are analyzed by a cycle, and the necessary manipulation takes place.

BUT this code does not take into account that there can be subdirectories in the directory It is necessary for me that subdirectories are also taken into account (and the depth of the subdirectories can be different).

I hope described is available, can anyone help :)

  • The surest way is to recurse through all the directories. That is: 1. Get a list of files 2. Start busting 3. If this is a directory, see paragraph 1. If the question survives the morning without an answer, I will write a solution) - Roman Danilov

1 answer 1

You can probably replace the non-recursive traversal of files in a folder:

 for (File file : directory.listFiles()) { ... } 

to recursive using Java 8 method Files::walk :

 Files.walk(directory.toPath()) .map(Path::toFile) .filter(file -> file.getName().endsWith(".php")) .forEach(file -> { // тут код обработки файла }); 

Well, or here's one of the non-Java 8 solutions :

 List<File> allFiles = new ArrayList<>(); Queue<File> fileTree = new PriorityQueue<>(); Collections.addAll(fileTree, directory.listFiles()); while (!fileTree.isEmpty()) { File currentFile = fileTree.remove(); if(currentFile.isDirectory()){ Collections.addAll(fileTree, currentFile.listFiles()); } else { allFiles.add(currentFile); } } for (File file : allFiles) { // тут код обработки файла // в том числе проверка на endsWith(".php") } 
  • And the old method is an old-fashioned example? For, as I understand, java8 is not supported by this plugin, which I do :) - Manitikyl
  • one
    @Manitikyl, excuse me, but it is impossible (I even started writing it, but I really didn’t like it that I would have to create a new method, and I’ll have to send a callback to it, and for that callback I’ll have to create a new class ...). So you can safely take a plus and wait until tomorrow morning, when Roman writes) - diraria
  • And if you make a crutch in the form of a FOR loop, which would be well there 10 times would look deep. It seems like you can gash. Or is it really quite hardcore to be? - Manitikyl
  • @Manitikyl, I think it's better not to do that. If anything, I updated the answer. - diraria September