Friday, 6 October 2023

EM-Tirupati Codeathon Series #07

[Question]
WINDOWS DIRECTORY SEARCH
Your program will accept a valid windows directory as theinput. If the directory is not valid, you have to show a message saying that 'Directory Not Found on the Filesystem Else, you have to find out all txt files and .exe files in your directory and then add it to a java collection in such a way that it stores it in a sorted way (sorted by the directory name) where the key is the fully qualified directory name and the values must be the list of all .txt and .exe files. You have to do the same for all child directories that are found in the parent directory, until the time no directories remain to traverse.
 
Filesystem (Sample)
c:\files
      file1.txt
      file2.exe
      file3.bat
  \filex
           \filez
\filey
       file 4.txt
       file5.exe
       \filef
file6.txt
 file7.exe
 \fileg
 
[Sample Input]
 c:\files
 
[Sample Output]
c:\files              file1.txt, file2.exe
c:\files\filex                              
c:\files\filexz\filez                      
c:\files\filey       file4.txt, file5.exe                       
c:\files\filey\filef\         file6.txt, file7.exe
c:files\filey\filef\fileg
 
Explanation of the Output
You have to make sure that you traverse every sub-directory that is inside the input directory and list the .txt and .exe in the format as shown above. You have to make sure no sub-directory should be left out and that all levels of child sub-directories are traversed. If no matching file is found, then a blank output should be printed as shown above. Please strictly stick to the output format as given above.
 
[Explanation of Solution]
This program takes a valid Windows directory as input and checks if it exists; if not, it displays a "Directory Not Found" message. If the directory is valid, it searches for .txt and .exe files within it and its subdirectories. The program stores the results in a sorted Java collection called fileMap, where each entry has a fully qualified directory name as the key and a list of matching .txt and .exe files as the values. Finally, the program prints the directory hierarchy along with the names of .txt and .exe files.
 
Areas: Java Collections, Core Java, Logic, Data Structures, Algorithms
 
Github Repo for Code:  https://tinyurl.com/ht7dzc6e
 
[Solution (Java Code)]
/*
 * Core Java Problem Solving - Codeathon.
 * A.R. Kishore Kumar ~ (c) 2023 ~ -- ~ Tirupati, Andhra Pradesh, India ~
 *
 */
package codeathon;
import java.io.File;
import java.util.*;
 
public class Codeathon07_Kishore
{
    public static void main(String[] args)
    {
        Scanner scanner = new Scanner(System.in);
        String directoryPath = scanner.nextLine();
        File directory = new File(directoryPath);
        if (!directory.exists() || !directory.isDirectory())
        {
            System.out.println("Directory Not Found on the Filesystem");
            return;
        }
        Map<String, List<String>> fileMap = WinDirectorySearch.listFiles(directory);
 
        DisplayFileDetails.printFileMap(fileMap);
    }
}
class WinDirectorySearch
{
    // Method to store fully qualified directory name as the key
    // and a list of .txt and .exe files as the values
    public static Map<String, List<String>> listFiles(File directory)
    {
        Map<String, List<String>> fileMap = new TreeMap<>();
        File[] files = directory.listFiles();
        if (files != null)
        {
            List<String> txtAndExeFiles = new ArrayList<>();
            for (File file : files)
            {
                if (file.isDirectory())
                {
                    Map<String, List<String>> childFileMap = listFiles(file);
                    if (!childFileMap.isEmpty())
                    {
                        fileMap.putAll(childFileMap);
                    }
                }
                else
                {
                    String fileName = file.getName().toLowerCase();
                    if (fileName.endsWith(".txt") || fileName.endsWith(".exe"))
                    {
                        txtAndExeFiles.add(file.getName());
                    }
                }
            }
            if (!txtAndExeFiles.isEmpty())
            {
                fileMap.put(directory.getAbsolutePath(), txtAndExeFiles);
            }
            else
            {
                fileMap.put(directory.getAbsolutePath(), new ArrayList<>());
            }
        }
        return fileMap;
    }
}
 
class DisplayFileDetails
{
    // Method that displays the directory hierarchy with file names
    public static void printFileMap(Map<String, List<String>> fileMap)
    {
        for (String dirName : fileMap.keySet())
        {
            List<String> files = fileMap.get(dirName);
            System.out.print(dirName);
            if (!files.isEmpty())
            {
                System.out.print("\t" + String.join(", ", files));
            }
            System.out.println();
        }
    }
}

No comments:

Post a Comment