[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
Explanation:
This program takes a valid Windows
directory as input and checks if it exists. If not found, it displays a "Directory
Not Found" message. Otherwise, it recursively searches for .txt and .exe
files in the directory and its subdirectories, storing them in a sorted Java
collection.
The collection is structured with
the fully qualified directory name as the key and a list of .txt and .exe files
as the values. It continues this process for all child directories until there
are no more directories to traverse.
The output displays the directory hierarchy with file names, sorted by directory name. Each line represents a directory and lists the corresponding .txt and .exe files found within it.
[Explaination of the solution]
In this problem give the input as directory and find the text and exe files in that directory.
Github Repo for Code:
[Solution (Java Code)]
package codeathon;
import java.io.File;
import java.util.*;
public class Codeathon07_vijay {
public static Map<String, List<String>> traverseAndCollectFiles(String directoryPath) {
Map<String, List<String>> filesMap = new HashMap<>();
traverseDirectory(directoryPath, filesMap);
return filesMap;
}
private static void traverseDirectory(String directoryPath, Map<String, List<String>> filesMap) {
File directory = new File(directoryPath);
if (!directory.exists() || !directory.isDirectory()) {
System.out.println("Directory Not Found on the Filesystem");
return;
}
File[] files = directory.listFiles();
List<String> txtFiles = new ArrayList<>();
List<String> exeFiles = new ArrayList<>();
for (File file : files) {
if (file.isFile()) {
String fileName = file.getName();
if (fileName.endsWith(".txt")) {
txtFiles.add(fileName);
} else if (fileName.endsWith(".exe")) {
exeFiles.add(fileName);
}
}
}
String fullPath = directory.getAbsolutePath();
if (!txtFiles.isEmpty() || !exeFiles.isEmpty()) {
List<String> fileList = new ArrayList<>();
fileList.addAll(txtFiles);
fileList.addAll(exeFiles);
filesMap.put(fullPath, fileList);
}
for (File file : files) {
if (file.isDirectory()) {
traverseDirectory(file.getAbsolutePath(), filesMap);
}
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the directory path: ");
String directoryPath = scanner.nextLine();
Map<String, List<String>> filesMap = traverseAndCollectFiles(directoryPath);
for (Map.Entry<String, List<String>> entry : filesMap.entrySet()) {
System.out.print(entry.getKey());
List<String> filesList = entry.getValue();
if (!filesList.isEmpty()) {
System.out.print("\t");
for (String file : filesList) {
System.out.print(file + ", ");
}
System.out.println();
} else {
System.out.println();
}
}
scanner.close();
}
}
Thank you,
k.vijay(Intern)
vijay.keradhi@eminds.ai
Enterprise Minds.
No comments:
Post a Comment