Monday, October 9, 2023

Codeathon07_Haritha


[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:\filesfile1.txt, file2.exe

c:\files\filex                             

c:\files\filexz\filez                      

c:\files\filey       file4.txt, file5.exe                       

c:\files\filey\fileffile6.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.

Program

package src;

import java.io.File;

import java.util.*;

public class Codeathon07_Haritha {

  public static void main(String[] args) {

    Scanner scanner = new Scanner(System.in);

    System.out.print("Enter a valid Windows directory: ");

    String inputDirectory = scanner.nextLine();

     File rootDirectory = new File(inputDirectory); // Create a File object representing the root directory
     
      provided by the user

     if (!rootDirectory.exists() || !rootDirectory.isDirectory()) {

      System.out.println("Directory Not Found on the Filesystem");

      return;

    }

Map<String, List<String>> directoryMap = new TreeMap<>(); // Create a TreeMap to store directory paths and corresponding

lists of files

 collectFiles(rootDirectory, directoryMap);

for (Map.Entry<String, List<String>> entry : directoryMap.entrySet()) {   // Print the collected directory paths and associated

file lists

   System.out.println("Directory: " + entry.getKey());

    System.out.println("Files: " + entry.getValue());

    }

  }

  private static void collectFiles(File directory, Map<String, List<String>> directoryMap) {

    File[] files = directory.listFiles();        // List all files and subdirectories in the current directory

  if (files != null) {

   List<String> txtAndExeFiles = new ArrayList<>();    // Create a list to store names of .txt and .exe files

   for (File file : files) {

        if (file.isFile() && (file.getName().endsWith(".txt") || file.getName().endsWith(".exe"))) {

          txtAndExeFiles.add(file.getName());    // If the file has a .txt or .exe extension, add the name of it

        } else if (file.isDirectory()) {

          collectFiles(file, directoryMap);

        }

      }

   if (!txtAndExeFiles.isEmpty()) {

        directoryMap.put(directory.getAbsolutePath(), txtAndExeFiles);

      }

      for (File subDirectory : files) {

        if (subDirectory.isDirectory()) {

          collectFiles(subDirectory, directoryMap);

        }

      }

    }

  }

}

[Explanation of Solution]

1. The program's purpose is to scan a user-provided Windows directory, collect information about .txt and .exe files in that directory and its subdirectories, and display this information in a structured format.

2. The code uses the java.io.File class to work with directories and files.

3. It starts by taking user input for a valid Windows directory.

4. It creates a File object called rootDirectory to represent the root directory provided by the user.

5. It checks whether the provided directory exists and is indeed a directory on the filesystem. If not, it displays an error message and exits the program.

6. A Map<String, List<String>> named directoryMap is created to store directory paths as keys and lists of associated .txt and .exe files as values. It uses a TreeMap to automatically sort the directory paths alphabetically.

7. The collectFiles method is called, passing the rootDirectory and directoryMap as parameters to recursively collect file information.

Inside the collectFiles method:

1. It lists all files and subdirectories in the current directory using directory.listFiles().

2. It initializes a list called txtAndExeFiles to store the names of .txt and .exe files.

3. It iterates through the files and checks if each file is a .txt or .exe file based on its file extension. If so, it adds the file name to txtAndExeFiles.

4. If the current file is a subdirectory, it recursively calls the collectFiles method on that subdirectory.

5. After processing all files in the current directory, if there are .txt or .exe files in the txtAndExeFiles list, it associates the current directory's absolute path with the list of files in directoryMap.

6. It then continues to process any subdirectories in the current directory.

Finally, after all files and subdirectories have been processed, the code iterates through directoryMap, displaying the collected directory paths and associated lists of .txt and .exe files in a structured format.





Haritha .P(Intern),
Guard Ninjas,
Data Shield Team,
Enterprise Minds.   








0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home