Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Files
- # Files Utils
- >Hint: You can use the File class from the standard Java library for the implementation of the utils below (https://www.geeksforgeeks.org/file-class-in-java/).
- ## 1. Directory Traversal
- Write a recursive method `void traverseDirectories(String path)` which prints the names of all files in the given `path`
- ### Example usage
- ```java
- String path = Paths.get("demo").toAbsolutePath().toString();
- FilesUtils.traverseDirectories(path);
- ```
- ### Expected output
- ```
- demo:
- nested-1:
- file-1.md
- file-2.md
- file-3.md
- nested-1-1:
- example.txt
- nested-2:
- test-1.csv
- test-2.txt
- test-3.md
- ```
- ## 2. Find Files
- Write method `List<String> findFiles(String path, String extension)` which return a collection of all files which have the given `extension` in the given directory.
- ### Example usage
- ```java
- String path = Paths.get("demo").toAbsolutePath().toString();
- System.out.println(findFiles(path, "md"));
- ```
- ### Expected output
- ```
- [file-1.md, file-2.md, file-3.md, test-3.md]
- ```
- ### 3. File Exists
- Write a recursive method `boolean fileExists(String path, String fileName)`
- ### Example usage
- ```java
- String path = Paths.get("demo").toAbsolutePath().toString();
- System.out.println(fileExists(path, "test-3.md"));
- System.out.println(fileExists(path, "test-2.md"));
- ```
- ### Expected Output
- ```
- true
- false
- ```
- ### 4. Directory Stats
- Write a recursive method `Map<String, Integer> getDirectoryStats(String path)` which returns the number of files for each file extension.
- ### Example usage
- ```java
- String path = Paths.get("demo").toAbsolutePath().toString();
- System.out.println(FilesUtils.getDirectoryStats(path));
- ```
- ### Expected Output
- ```
- {txt=2, md=4, csv=1}
- ```
- public class FilesUtils {
- public static Map<String, Integer> directoryStats = new HashMap<>();
- public static void traverseDirectories(String path) {
- File dir = new File(path);
- File[] allFiles = dir.listFiles();
- if (allFiles == null) {
- return;
- }
- if (allFiles.length == 0) {
- return;
- }
- System.out.println(dir.getName() + ":");
- String indent = "";
- traverseRecursive(allFiles, indent);
- }
- private static void traverseRecursive(File[] list, String indent) {
- if (list == null) return;
- if (list.length == 0) return;
- indent += " ";
- for (File file : list) {
- if (file.isDirectory()) {
- System.out.println(indent + file.getName() + ":");
- traverseRecursive(file.listFiles(), indent);
- }
- if (file.isFile()) {
- System.out.println(indent + file.getName());
- }
- }
- }
- public static List<String> findFiles(String path, String extension) {
- File fileSearch = new File(path);
- File[] allFiles = fileSearch.listFiles();
- List<String> result = new ArrayList<>();
- if (allFiles == null) {
- return result;
- }
- if (allFiles.length == 0) {
- return result;
- }
- for (File file : allFiles) {
- if (file.isDirectory()) {
- result.addAll(findFiles(file.toString(), extension));
- } else {
- if (file.getName().endsWith(extension)) {
- result.add(file.getName());
- }
- }
- }
- return result;
- }
- public static boolean fileExists(String path, String fileName) {
- File dir = new File(path);
- File[] allFilesInDir = dir.listFiles();
- boolean fileNameExist = false;
- if (allFilesInDir == null || allFilesInDir.length == 0) {
- return false;
- }
- for (File file : allFilesInDir) {
- if (file.isDirectory()) {
- fileNameExist = fileExists(file.toString(), fileName);
- }
- if (file.getName().equals(fileName)) {
- return true;
- }
- }
- return fileNameExist;
- }
- public static Map<String, Integer> getDirectoryStats(String path) {
- File dir = new File(path);
- File[] allFilesInDir = dir.listFiles();
- for (File file : allFilesInDir) {
- String temp = file.getName().substring(file.getName().indexOf(".") + 1);
- if(file.isDirectory()){
- getDirectoryStats(file.toString());
- }
- if(file.isFile() && directoryStats.containsKey(temp)){
- directoryStats.replace(temp, directoryStats.get(temp) + 1);
- }
- if(file.isFile() && !directoryStats.containsKey(temp)){
- directoryStats.put(temp, 1);
- }
- }
- return directoryStats;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment