JetBrains Academy: File hierarchies - Kamil-Jankowski/Learning-JAVA GitHub Wiki

JetBrains Academy: File hierarchies

Find all empty directories:

Download and unpack the following archive: https://stepik.org/media/attachments/lesson/91407/basedir.zip.

If you open it, you will see a lot of directories containing subdirectories or/and files. A subdirectory may be empty or may contain some files.

Your task is to write a program that finds all empty subdirectories in this hierarchy.

Enter the names of the directories separated by spaces in any order.

    private static void getEmptyDir(File directory) {
        File[] folders = directory.listFiles();

        if (folders.length == 0) {
            System.out.println(directory.getName());
        }

        for (File folder : folders) {
            if (folder.listFiles() != null) {
                getEmptyDir(folder);
            }
        }
    }

Find a directory with the max number of files:

Download and unpack the following archive: https://stepik.org/media/attachments/lesson/91404/basedir.zip.

If you unpack and open it, you will see a lot of directories containing files.

You must write a program that finds a directory with the maximum number of files.

Enter the name of the directory and the number of files in it separated by one space.

    static int count = 0;
    static int max = 0;

    private static void getFile(File dirPath) {
        File[] files = dirPath.listFiles();

        if (files != null) {
            for (File value : files) {
                if (value.isDirectory()) {
                    getFile(value);
                } else {
                    count++;
                }
            }

            if (count > max) {
                max = count;
                System.out.println("Directory " + dirPath + " has " + max + " elements");
            }
            count = 0;
        }
    }

The deepest file:

Download and unpack the following archive: https://stepik.org/media/attachments/lesson/91426/basedir.zip.

If you open it, you will see a hierarchy of files.

Write a program that finds the deepest file (or a directory) in this hierarchy.

Enter the name of the file you will get.

        Path path = Paths.get("basedir3");
        Map<String, String> files;
        try {
            files = Files.walk(path)
                         .map(e -> new File(String.valueOf(e)))
                         .filter(File::isFile)
                         .collect(Collectors.toMap(File::getName, File::getAbsolutePath));

            int depth = 0;
            int maxDepth = 0;
            String deepestFile = "";

            for (String file : files.keySet()) {
                String absolutePath = files.get(file);
                depth = findDepth(absolutePath);
                if (depth > maxDepth) {
                    maxDepth = depth;
                    deepestFile = absolutePath;
                }
                depth = 0;
            }

            System.out.println(deepestFile + " depth: " + maxDepth);

        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    private static int findDepth(String path) {
        String[] levels = path.split("\\\\");
        return levels.length;
    }